****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Process CRSP & Compustat data
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
clear
use gvkey_str lpermno lpermco linkdt linkenddt cusip tic using crspCompustatLinking
	rename (lpermno lpermco cusip gvkey_str) (permno permco cusip_header gvkey)
	duplicates drop
	sort permno permco linkdt linkenddt 
	by permno permco: gen noGvkeys=_N
	sort gvkey linkdt linkenddt 
	by gvkey: gen noPermnos=_N
tempfile gvkeys	
save `gvkeys'

* CRSP Linking
clear
use permco permno ncusip cusip date nameendt ticker comnam using crspMonthly
	*bysort permco permno ncusip cusip date nameendt ticker: gen dup=cond(_N>1,1,0)
	*gsort -dup permco permno ncusip cusip date nameendt ticker
	*keep permco permno ncusip cusip date nameendt ticker 
	duplicates drop
	joinby permco permno using `gvkeys', unm(b)
	drop if _merge == 2
	*gen compuMatch =cond(_merge==3,1,0)
	drop _merge	noPermnos
	replace noGvkeys=0 if noGvkeys==.
	sort permno date 
	gen keeper=1
	gen yrMth = ym(year(date), month(date))
	gen linkYrMth = ym(year(linkdt), month(linkdt))
	gen linkEndYrMth = ym(year(linkenddt), month(linkenddt))
	format %tm yrMth 
	*replace keeper=0 if noGvkeys>1 & (date<linkdt | date>linkenddt )
	replace keeper=0 if noGvkeys>1 & (yrMth<linkYrMth | yrMth>linkEndYrMth)
	drop if keeper==0
	*bysort permno date: gen dup=_N
	*tab dup
	*gsort -dup permno date
	drop keeper
	gen yr=year(date)
	*duplicates drop permco yrMth gvkey permno ncusip cusip tic ticker, force
	drop if ncusip==""
	gen issuer_cusip = substr(ncusip,1,6)
	duplicates drop permco yr yrMth issuer_cusip gvkey, force
	bysort permco issuer_cusip yr yrMth : drop if _N>1
	*bysort issuer_cusip yr yrMth: gen dup=_N 
	*tab dup
save crspMonthlyLink, replace	
	
* Compustat Linking	
clear
use gvkey datadate fyear cusip conm using compuAnnual	
	duplicates drop 
	joinby gvkey using `gvkeys', unm(b)
	drop if _merge == 2
	drop _merge	noGvkeys
	replace noPermnos=0 if noPermnos==.
	sort permno datadate 
	gen keeper=1
	gen yrMth = ym(year(datadate), month(datadate))
	gen linkYrMth = ym(year(linkdt), month(linkdt))
	gen linkEndYrMth = ym(year(linkenddt), month(linkenddt))
	format %tm yrMth 
	*replace keeper=0 if noGvkeys>1 & (date<linkdt | date>linkenddt )
	replace keeper=0 if noPermnos>1 & (yrMth<linkYrMth | yrMth>linkEndYrMth)
	drop if keeper==0
	gen yr = year(datadate)
	*bysort permno date: gen dup=_N
	*tab dup
	*gsort -dup permno date
	*duplicates drop permco permno yr yrMth gvkey cusip, force
	gen issuer_cusip_head = substr(cusip,1,6)
	duplicates drop permco yr yrMth issuer_cusip_head gvkey, force
	bysort permco gvkey yr yrMth : drop if _N>1
save compuAnnualLink, replace	

* Bond-CRSP linkinf file from WRDS
clear
use bondCRSPLink
	keep cusip permco crsp_startdt crsp_enddt
save bondCRSPLink_shrt, replace

* Quarterly Financials to be merged with Final FISD+TRACE+SDC sample
clear
use compuQtrly
	gen calYrQtr=yq(fyearq,fqtr)
	format %tq calYrQtr
	bysort gvkey calYrQtr: gen dup=cond(_N==1,1,_N)
	sort gvkey calYrQtr datadate
	bysort gvkey calYrQtr: keep if _n==_N	
	gen gvkeyNo=real(gvkey) 
	tsset gvkeyNo calYrQtr
	gen roa = oibdpq/((atq+l.atq)/2)
	gen mktval= cond(mkvaltq!=.,mkvaltq,abs(prccq)*cshoq)	
	gen bkvlpsq=ceqq/cshoq
	gen mb = mktval/ceqq
	gen epsq=niq/cshoq
	gen saleGrowth=(saleq/l.saleq)-1
	gen ret1Qtr=(abs(prccq)/abs(l.prccq))-1	
	replace atq=. if atq<=0 
	replace cshoq=. if cshoq==0
	gen tobinsq=(atq+(prccq*cshoq)-ceqq)/atq
	gen pe=mktval/niq
	gen lev=(dlcq+dlttq)/atq
	gen cash_at=chq_ef/atq_ef
	
	la var tobinsq "Tobins Q\$ _{t-1} \$"
	la var pe "P\/E ratio"
	la var roa "Return on Assets\$ _{t-1} \$"
	la var icaptq "Invested Capital\$ _{t-1} \$"
	la var mb "Market-to-Book\$ _{t-1} \$"
	la var ret1Qtr "Stock Return\$ _{t-1} \$"
	la var saleGrowth "Sales Growth\$ _{t-1} \$"
save compuData, replace	

****************************************************************************************************************************************************************
* SDC Data Prep
****************************************************************************************************************************************************************

clear
use permco gvkey yr issuer_cusip_head using compuAnnualLink
	duplicates drop gvkey yr, force
tempfile compuLink	
save `compuLink'

clear
use sdcMnA_1990_2019
	gen dateannounced1=mdy(real(substr(dateannounced,1,2)),real(substr(dateannounced,4,2)),real(substr(dateannounced,7,2))+2000)
	gen dateeffective1=mdy(real(substr(dateeffective,1,2)),real(substr(dateeffective,4,2)),real(substr(dateeffective,7,2))+2000)
	gen datewithdrawn1=mdy(real(substr(datewithdrawn,1,2)),real(substr(datewithdrawn,4,2)),real(substr(datewithdrawn,7,2))+2000)
	gen yr=year(dateannounced1)	
	
	replace dateannounced1=mdy(real(substr(dateannounced,1,2)),real(substr(dateannounced,4,2)),real(substr(dateannounced,7,2))+1900) if year(dateannounced1)>2019
	replace dateeffective1=mdy(real(substr(dateeffective,1,2)),real(substr(dateeffective,4,2)),real(substr(dateeffective,7,2))+1900) if yr>2019
	replace datewithdrawn1=mdy(real(substr(datewithdrawn,1,2)),real(substr(datewithdrawn,4,2)),real(substr(datewithdrawn,7,2))+1900) if yr>2019
	drop yr
	
	format %td dateannounced1 dateeffective1 datewithdrawn1 
	drop dateannounced dateeffective datewithdrawn 
	rename  (dateannounced1 dateeffective1 datewithdrawn1 ) (dateannounced dateeffective datewithdrawn )
	gen yrMth=ym(year(dateannounced), month(dateannounced))
	format %tm yrMth
	gen yr=year(dateannounced)
	replace acqcusip=trim(acqcusip)
	replace trgtcusip=trim(trgtcusip)	
	
	rename acqcusip issuer_cusip
	merge m:1 issuer_cusip yrMth using crspMonthlyLink, keepusing(linkYrMth linkEndYrMth gvkey permco permno)
	drop if _merge==2
	gen crspLink_acq=cond(_merge==3,1,0)
	drop _merge 
	merge m:1 gvkey yr using `compuLink', keepusing(issuer_cusip_head)
	drop if _merge==2
	gen compuLink_acq=cond(_merge==3,1,0)
	drop _merge	issuer_cusip_head
	rename (linkYrMth linkEndYrMth gvkey permco permno) (linkYrMth_acq linkEndYrMth_acq gvkey_acq permco_acq permno_acq)
	
	rename (issuer_cusip trgtcusip) (acqcusip issuer_cusip)
	merge m:1 issuer_cusip yrMth using crspMonthlyLink, keepusing(linlinkEndYrMth gvkey permco permno)
	drop if _merge==2
	gen crspLink_trgt=cond(_merge==3,1,0)
	drop _merge 
	merge m:1 gvkey yr using `compuLink', keepusing(issuer_cusip_head)
	drop if _merge==2
	gen compuLink_trgt=cond(_merge==3,1,0)
	drop _merge	issuer_cusip_head
	rename (linkYrMth linkEndYrMth gvkey permco permno) (linkYrMth_trgt linkEndYrMth_trgt gvkey_trgt permco_trgt permno_trgt)
	rename (issuer_cusip) (trgtcusip)
	
	la var rankdate "Rank Date"
	la var dateannounced "Date Announced"
	la var dateeffective "Date Effective"
	la var dateeff_uncond "Date Effective/ Unconditional"
	la var datewithdrawn "Date Withdrawn"
	la var trgtname "Target Name"
	la var trgtprisic "Target Primary SIC Code"
	la var trgtindsector "Target Industry Sector"
	la var trgtstate "Target State"
	la var trgtnation "Target Nation"
	la var acqname "Acquiror Name"
	la var acqindsector "Acquiror Industry Sector"
	la var acqprisic "Acquiror Primary SIC Code"
	la var acqstate "Acquiror State"
	la var acqnation "Acquiror Nation"
	la var synopsis "Synopsis"
	la var history "History File Event"
	la var status "Status"
	la var pctsharesacq "% of Shares Acq."
	la var pctownedafter "% Owned After Trans- action"
	la var pctsought "% sought"
	la var acqcusip "Acquiror CUSIP"
	la var acqparentcusip "Acquiror Immediate Parent CUSIP"
	la var acqultparentcusip "Acquiror Ultimate Parent CUSIP"
	la var investorcusip "Investor Cusip"
	la var trgtcusip "Target CUSIP"
	la var trgtultparentcusip "Target Ultimate Parent CUSIP"
	la var trgtparentcusip "Target Immediate Parent CUSIP"
save sdcMnA_linked_1990_2019, replace

	
* Industry Identification

cd "D:\Datasets\TRACE\data"
clear
use sdcMnA_linked_1990_2019
	* Target Industry classification
	gen sic4_trg = real(trgtprisic)
	* French 12 industries
	gen trg_indCode=12	
	replace trg_indCode=1 if (sic4_trg>=100&sic4_trg<=999)
	replace trg_indCode=1 if (sic4_trg>=2000&sic4_trg<=2399)
	replace trg_indCode=1 if (sic4_trg>=2700&sic4_trg<=2749)
	replace trg_indCode=1 if (sic4_trg>=2770&sic4_trg<=2799)
	replace trg_indCode=1 if (sic4_trg>=3100&sic4_trg<=3199)
	replace trg_indCode=1 if (sic4_trg>=3940&sic4_trg<=3989)
	replace trg_indCode=2 if (sic4_trg>=2500&sic4_trg<=2519)
	replace trg_indCode=2 if (sic4_trg>=2590&sic4_trg<=2599)
	replace trg_indCode=2 if (sic4_trg>=3630&sic4_trg<=3659)
	replace trg_indCode=2 if (sic4_trg>=3710&sic4_trg<=3711)
	replace trg_indCode=2 if (sic4_trg>=3714&sic4_trg<=3714)
	replace trg_indCode=2 if (sic4_trg>=3716&sic4_trg<=3716)
	replace trg_indCode=2 if (sic4_trg>=3750&sic4_trg<=3751)
	replace trg_indCode=2 if (sic4_trg>=3792&sic4_trg<=3792)
	replace trg_indCode=2 if (sic4_trg>=3900&sic4_trg<=3939)
	replace trg_indCode=2 if (sic4_trg>=3990&sic4_trg<=3999)
	replace trg_indCode=3 if (sic4_trg>=2520&sic4_trg<=2589)
	replace trg_indCode=3 if (sic4_trg>=2600&sic4_trg<=2699)
	replace trg_indCode=3 if (sic4_trg>=2750&sic4_trg<=2769)
	replace trg_indCode=3 if (sic4_trg>=3000&sic4_trg<=3099)
	replace trg_indCode=3 if (sic4_trg>=3200&sic4_trg<=3569)
	replace trg_indCode=3 if (sic4_trg>=3580&sic4_trg<=3629)
	replace trg_indCode=3 if (sic4_trg>=3700&sic4_trg<=3709)
	replace trg_indCode=3 if (sic4_trg>=3712&sic4_trg<=3713)
	replace trg_indCode=3 if (sic4_trg>=3715&sic4_trg<=3715)
	replace trg_indCode=3 if (sic4_trg>=3717&sic4_trg<=3749)
	replace trg_indCode=3 if (sic4_trg>=3752&sic4_trg<=3791)
	replace trg_indCode=3 if (sic4_trg>=3793&sic4_trg<=3799)
	replace trg_indCode=3 if (sic4_trg>=3830&sic4_trg<=3839)
	replace trg_indCode=3 if (sic4_trg>=3860&sic4_trg<=3899)
	replace trg_indCode=4 if (sic4_trg>=1200&sic4_trg<=1399)
	replace trg_indCode=4 if (sic4_trg>=2900&sic4_trg<=2999)
	replace trg_indCode=5 if (sic4_trg>=2800&sic4_trg<=2829)
	replace trg_indCode=5 if (sic4_trg>=2840&sic4_trg<=2899)
	replace trg_indCode=6 if (sic4_trg>=3570&sic4_trg<=3579)
	replace trg_indCode=6 if (sic4_trg>=3660&sic4_trg<=3692)
	replace trg_indCode=6 if (sic4_trg>=3694&sic4_trg<=3699)
	replace trg_indCode=6 if (sic4_trg>=3810&sic4_trg<=3829)
	replace trg_indCode=6 if (sic4_trg>=7370&sic4_trg<=7379)
	replace trg_indCode=7 if (sic4_trg>=4800&sic4_trg<=4899)
	replace trg_indCode=8 if (sic4_trg>=4900&sic4_trg<=4949)
	replace trg_indCode=9 if (sic4_trg>=5000&sic4_trg<=5999)
	replace trg_indCode=9 if (sic4_trg>=7200&sic4_trg<=7299)
	replace trg_indCode=9 if (sic4_trg>=7600&sic4_trg<=7699)
	replace trg_indCode=10 if (sic4_trg>=2830&sic4_trg<=2839)
	replace trg_indCode=10 if (sic4_trg>=3693&sic4_trg<=3693)
	replace trg_indCode=10 if (sic4_trg>=3840&sic4_trg<=3859)
	replace trg_indCode=10 if (sic4_trg>=8000&sic4_trg<=8099)
	replace trg_indCode=11 if (sic4_trg>=6000&sic4_trg<=6999)
	label define trg_indNames 1 "Non Durables" 2 "Durables" 3 "Manufacturing" 4 "Energy" 5 "Chemicals" 6 "Business Equipment" 7 "Telecoms" 8 "Utilities" 9 "Wholesales" 10 "Healthcare" 11 "Finance" 12 "Others"
	label values trg_indCode indNames
	
	* French 48 industries
	gen trg_indCode48=48	
	replace trg_indCode48=1 if (sic4_trg>=100&sic4_trg<=199)
	replace trg_indCode48=1 if (sic4_trg>=200&sic4_trg<=299)
	replace trg_indCode48=1 if (sic4_trg>=700&sic4_trg<=799)
	replace trg_indCode48=1 if (sic4_trg>=910&sic4_trg<=919)
	replace trg_indCode48=1 if (sic4_trg>=2048&sic4_trg<=2048)
	replace trg_indCode48=2 if (sic4_trg>=2000&sic4_trg<=2009)
	replace trg_indCode48=2 if (sic4_trg>=2010&sic4_trg<=2019)
	replace trg_indCode48=2 if (sic4_trg>=2020&sic4_trg<=2029)
	replace trg_indCode48=2 if (sic4_trg>=2030&sic4_trg<=2039)
	replace trg_indCode48=2 if (sic4_trg>=2040&sic4_trg<=2046)
	replace trg_indCode48=2 if (sic4_trg>=2050&sic4_trg<=2059)
	replace trg_indCode48=2 if (sic4_trg>=2060&sic4_trg<=2063)
	replace trg_indCode48=2 if (sic4_trg>=2070&sic4_trg<=2079)
	replace trg_indCode48=2 if (sic4_trg>=2090&sic4_trg<=2092)
	replace trg_indCode48=2 if (sic4_trg>=2095&sic4_trg<=2095)
	replace trg_indCode48=2 if (sic4_trg>=2098&sic4_trg<=2099)
	replace trg_indCode48=3 if (sic4_trg>=2064&sic4_trg<=2068)
	replace trg_indCode48=3 if (sic4_trg>=2086&sic4_trg<=2086)
	replace trg_indCode48=3 if (sic4_trg>=2087&sic4_trg<=2087)
	replace trg_indCode48=3 if (sic4_trg>=2096&sic4_trg<=2096)
	replace trg_indCode48=3 if (sic4_trg>=2097&sic4_trg<=2097)
	replace trg_indCode48=4 if (sic4_trg>=2080&sic4_trg<=2080)
	replace trg_indCode48=4 if (sic4_trg>=2082&sic4_trg<=2082)
	replace trg_indCode48=4 if (sic4_trg>=2083&sic4_trg<=2083)
	replace trg_indCode48=4 if (sic4_trg>=2084&sic4_trg<=2084)
	replace trg_indCode48=4 if (sic4_trg>=2085&sic4_trg<=2085)
	replace trg_indCode48=5 if (sic4_trg>=2100&sic4_trg<=2199)
	replace trg_indCode48=6 if (sic4_trg>=920&sic4_trg<=999)
	replace trg_indCode48=6 if (sic4_trg>=3650&sic4_trg<=3651)
	replace trg_indCode48=6 if (sic4_trg>=3652&sic4_trg<=3652)
	replace trg_indCode48=6 if (sic4_trg>=3732&sic4_trg<=3732)
	replace trg_indCode48=6 if (sic4_trg>=3930&sic4_trg<=3931)
	replace trg_indCode48=6 if (sic4_trg>=3940&sic4_trg<=3949)
	replace trg_indCode48=7 if (sic4_trg>=7800&sic4_trg<=7829)
	replace trg_indCode48=7 if (sic4_trg>=7830&sic4_trg<=7833)
	replace trg_indCode48=7 if (sic4_trg>=7840&sic4_trg<=7841)
	replace trg_indCode48=7 if (sic4_trg>=7900&sic4_trg<=7900)
	replace trg_indCode48=7 if (sic4_trg>=7910&sic4_trg<=7911)
	replace trg_indCode48=7 if (sic4_trg>=7920&sic4_trg<=7929)
	replace trg_indCode48=7 if (sic4_trg>=7930&sic4_trg<=7933)
	replace trg_indCode48=7 if (sic4_trg>=7940&sic4_trg<=7949)
	replace trg_indCode48=7 if (sic4_trg>=7980&sic4_trg<=7980)
	replace trg_indCode48=7 if (sic4_trg>=7990&sic4_trg<=7999)
	replace trg_indCode48=8 if (sic4_trg>=2700&sic4_trg<=2709)
	replace trg_indCode48=8 if (sic4_trg>=2710&sic4_trg<=2719)
	replace trg_indCode48=8 if (sic4_trg>=2720&sic4_trg<=2729)
	replace trg_indCode48=8 if (sic4_trg>=2730&sic4_trg<=2739)
	replace trg_indCode48=8 if (sic4_trg>=2740&sic4_trg<=2749)
	replace trg_indCode48=8 if (sic4_trg>=2770&sic4_trg<=2771)
	replace trg_indCode48=8 if (sic4_trg>=2780&sic4_trg<=2789)
	replace trg_indCode48=8 if (sic4_trg>=2790&sic4_trg<=2799)
	replace trg_indCode48=9 if (sic4_trg>=2047&sic4_trg<=2047)
	replace trg_indCode48=9 if (sic4_trg>=2391&sic4_trg<=2392)
	replace trg_indCode48=9 if (sic4_trg>=2510&sic4_trg<=2519)
	replace trg_indCode48=9 if (sic4_trg>=2590&sic4_trg<=2599)
	replace trg_indCode48=9 if (sic4_trg>=2840&sic4_trg<=2843)
	replace trg_indCode48=9 if (sic4_trg>=2844&sic4_trg<=2844)
	replace trg_indCode48=9 if (sic4_trg>=3160&sic4_trg<=3161)
	replace trg_indCode48=9 if (sic4_trg>=3170&sic4_trg<=3171)
	replace trg_indCode48=9 if (sic4_trg>=3172&sic4_trg<=3172)
	replace trg_indCode48=9 if (sic4_trg>=3190&sic4_trg<=3199)
	replace trg_indCode48=9 if (sic4_trg>=3229&sic4_trg<=3229)
	replace trg_indCode48=9 if (sic4_trg>=3260&sic4_trg<=3260)
	replace trg_indCode48=9 if (sic4_trg>=3262&sic4_trg<=3263)
	replace trg_indCode48=9 if (sic4_trg>=3269&sic4_trg<=3269)
	replace trg_indCode48=9 if (sic4_trg>=3230&sic4_trg<=3231)
	replace trg_indCode48=9 if (sic4_trg>=3630&sic4_trg<=3639)
	replace trg_indCode48=9 if (sic4_trg>=3750&sic4_trg<=3751)
	replace trg_indCode48=9 if (sic4_trg>=3800&sic4_trg<=3800)
	replace trg_indCode48=9 if (sic4_trg>=3860&sic4_trg<=3861)
	replace trg_indCode48=9 if (sic4_trg>=3870&sic4_trg<=3873)
	replace trg_indCode48=9 if (sic4_trg>=3910&sic4_trg<=3911)
	replace trg_indCode48=9 if (sic4_trg>=3914&sic4_trg<=3914)
	replace trg_indCode48=9 if (sic4_trg>=3915&sic4_trg<=3915)
	replace trg_indCode48=9 if (sic4_trg>=3960&sic4_trg<=3962)
	replace trg_indCode48=9 if (sic4_trg>=3991&sic4_trg<=3991)
	replace trg_indCode48=9 if (sic4_trg>=3995&sic4_trg<=3995)
	replace trg_indCode48=10 if (sic4_trg>=2300&sic4_trg<=2390)
	replace trg_indCode48=10 if (sic4_trg>=3020&sic4_trg<=3021)
	replace trg_indCode48=10 if (sic4_trg>=3100&sic4_trg<=3111)
	replace trg_indCode48=10 if (sic4_trg>=3130&sic4_trg<=3131)
	replace trg_indCode48=10 if (sic4_trg>=3140&sic4_trg<=3149)
	replace trg_indCode48=10 if (sic4_trg>=3150&sic4_trg<=3151)
	replace trg_indCode48=10 if (sic4_trg>=3963&sic4_trg<=3965)
	replace trg_indCode48=11 if (sic4_trg>=8000&sic4_trg<=8099)
	replace trg_indCode48=12 if (sic4_trg>=3693&sic4_trg<=3693)
	replace trg_indCode48=12 if (sic4_trg>=3840&sic4_trg<=3849)
	replace trg_indCode48=12 if (sic4_trg>=3850&sic4_trg<=3851)
	replace trg_indCode48=13 if (sic4_trg>=2830&sic4_trg<=2830)
	replace trg_indCode48=13 if (sic4_trg>=2831&sic4_trg<=2831)
	replace trg_indCode48=13 if (sic4_trg>=2833&sic4_trg<=2833)
	replace trg_indCode48=13 if (sic4_trg>=2834&sic4_trg<=2834)
	replace trg_indCode48=13 if (sic4_trg>=2835&sic4_trg<=2835)
	replace trg_indCode48=13 if (sic4_trg>=2836&sic4_trg<=2836)
	replace trg_indCode48=14 if (sic4_trg>=2800&sic4_trg<=2809)
	replace trg_indCode48=14 if (sic4_trg>=2810&sic4_trg<=2819)
	replace trg_indCode48=14 if (sic4_trg>=2820&sic4_trg<=2829)
	replace trg_indCode48=14 if (sic4_trg>=2850&sic4_trg<=2859)
	replace trg_indCode48=14 if (sic4_trg>=2860&sic4_trg<=2869)
	replace trg_indCode48=14 if (sic4_trg>=2870&sic4_trg<=2879)
	replace trg_indCode48=14 if (sic4_trg>=2890&sic4_trg<=2899)
	replace trg_indCode48=15 if (sic4_trg>=3031&sic4_trg<=3031)
	replace trg_indCode48=15 if (sic4_trg>=3041&sic4_trg<=3041)
	replace trg_indCode48=15 if (sic4_trg>=3050&sic4_trg<=3053)
	replace trg_indCode48=15 if (sic4_trg>=3060&sic4_trg<=3069)
	replace trg_indCode48=15 if (sic4_trg>=3070&sic4_trg<=3079)
	replace trg_indCode48=15 if (sic4_trg>=3080&sic4_trg<=3089)
	replace trg_indCode48=15 if (sic4_trg>=3090&sic4_trg<=3099)
	replace trg_indCode48=16 if (sic4_trg>=2200&sic4_trg<=2269)
	replace trg_indCode48=16 if (sic4_trg>=2270&sic4_trg<=2279)
	replace trg_indCode48=16 if (sic4_trg>=2280&sic4_trg<=2284)
	replace trg_indCode48=16 if (sic4_trg>=2290&sic4_trg<=2295)
	replace trg_indCode48=16 if (sic4_trg>=2297&sic4_trg<=2297)
	replace trg_indCode48=16 if (sic4_trg>=2298&sic4_trg<=2298)
	replace trg_indCode48=16 if (sic4_trg>=2299&sic4_trg<=2299)
	replace trg_indCode48=16 if (sic4_trg>=2393&sic4_trg<=2395)
	replace trg_indCode48=16 if (sic4_trg>=2397&sic4_trg<=2399)
	replace trg_indCode48=17 if (sic4_trg>=800&sic4_trg<=899)
	replace trg_indCode48=17 if (sic4_trg>=2400&sic4_trg<=2439)
	replace trg_indCode48=17 if (sic4_trg>=2450&sic4_trg<=2459)
	replace trg_indCode48=17 if (sic4_trg>=2490&sic4_trg<=2499)
	replace trg_indCode48=17 if (sic4_trg>=2660&sic4_trg<=2661)
	replace trg_indCode48=17 if (sic4_trg>=2950&sic4_trg<=2952)
	replace trg_indCode48=17 if (sic4_trg>=3200&sic4_trg<=3200)
	replace trg_indCode48=17 if (sic4_trg>=3210&sic4_trg<=3211)
	replace trg_indCode48=17 if (sic4_trg>=3240&sic4_trg<=3241)
	replace trg_indCode48=17 if (sic4_trg>=3250&sic4_trg<=3259)
	replace trg_indCode48=17 if (sic4_trg>=3261&sic4_trg<=3261)
	replace trg_indCode48=17 if (sic4_trg>=3264&sic4_trg<=3264)
	replace trg_indCode48=17 if (sic4_trg>=3270&sic4_trg<=3275)
	replace trg_indCode48=17 if (sic4_trg>=3280&sic4_trg<=3281)
	replace trg_indCode48=17 if (sic4_trg>=3290&sic4_trg<=3293)
	replace trg_indCode48=17 if (sic4_trg>=3295&sic4_trg<=3299)
	replace trg_indCode48=17 if (sic4_trg>=3420&sic4_trg<=3429)
	replace trg_indCode48=17 if (sic4_trg>=3430&sic4_trg<=3433)
	replace trg_indCode48=17 if (sic4_trg>=3440&sic4_trg<=3441)
	replace trg_indCode48=17 if (sic4_trg>=3442&sic4_trg<=3442)
	replace trg_indCode48=17 if (sic4_trg>=3446&sic4_trg<=3446)
	replace trg_indCode48=17 if (sic4_trg>=3448&sic4_trg<=3448)
	replace trg_indCode48=17 if (sic4_trg>=3449&sic4_trg<=3449)
	replace trg_indCode48=17 if (sic4_trg>=3450&sic4_trg<=3451)
	replace trg_indCode48=17 if (sic4_trg>=3452&sic4_trg<=3452)
	replace trg_indCode48=17 if (sic4_trg>=3490&sic4_trg<=3499)
	replace trg_indCode48=17 if (sic4_trg>=3996&sic4_trg<=3996)
	replace trg_indCode48=18 if (sic4_trg>=1500&sic4_trg<=1511)
	replace trg_indCode48=18 if (sic4_trg>=1520&sic4_trg<=1529)
	replace trg_indCode48=18 if (sic4_trg>=1530&sic4_trg<=1539)
	replace trg_indCode48=18 if (sic4_trg>=1540&sic4_trg<=1549)
	replace trg_indCode48=18 if (sic4_trg>=1600&sic4_trg<=1699)
	replace trg_indCode48=18 if (sic4_trg>=1700&sic4_trg<=1799)
	replace trg_indCode48=19 if (sic4_trg>=3300&sic4_trg<=3300)
	replace trg_indCode48=19 if (sic4_trg>=3310&sic4_trg<=3317)
	replace trg_indCode48=19 if (sic4_trg>=3320&sic4_trg<=3325)
	replace trg_indCode48=19 if (sic4_trg>=3330&sic4_trg<=3339)
	replace trg_indCode48=19 if (sic4_trg>=3340&sic4_trg<=3341)
	replace trg_indCode48=19 if (sic4_trg>=3350&sic4_trg<=3357)
	replace trg_indCode48=19 if (sic4_trg>=3360&sic4_trg<=3369)
	replace trg_indCode48=19 if (sic4_trg>=3370&sic4_trg<=3379)
	replace trg_indCode48=19 if (sic4_trg>=3390&sic4_trg<=3399)
	replace trg_indCode48=20 if (sic4_trg>=3400&sic4_trg<=3400)
	replace trg_indCode48=20 if (sic4_trg>=3443&sic4_trg<=3443)
	replace trg_indCode48=20 if (sic4_trg>=3444&sic4_trg<=3444)
	replace trg_indCode48=20 if (sic4_trg>=3460&sic4_trg<=3469)
	replace trg_indCode48=20 if (sic4_trg>=3470&sic4_trg<=3479)
	replace trg_indCode48=21 if (sic4_trg>=3510&sic4_trg<=3519)
	replace trg_indCode48=21 if (sic4_trg>=3520&sic4_trg<=3529)
	replace trg_indCode48=21 if (sic4_trg>=3530&sic4_trg<=3530)
	replace trg_indCode48=21 if (sic4_trg>=3531&sic4_trg<=3531)
	replace trg_indCode48=21 if (sic4_trg>=3532&sic4_trg<=3532)
	replace trg_indCode48=21 if (sic4_trg>=3533&sic4_trg<=3533)
	replace trg_indCode48=21 if (sic4_trg>=3534&sic4_trg<=3534)
	replace trg_indCode48=21 if (sic4_trg>=3535&sic4_trg<=3535)
	replace trg_indCode48=21 if (sic4_trg>=3536&sic4_trg<=3536)
	replace trg_indCode48=21 if (sic4_trg>=3538&sic4_trg<=3538)
	replace trg_indCode48=21 if (sic4_trg>=3540&sic4_trg<=3549)
	replace trg_indCode48=21 if (sic4_trg>=3550&sic4_trg<=3559)
	replace trg_indCode48=21 if (sic4_trg>=3560&sic4_trg<=3569)
	replace trg_indCode48=21 if (sic4_trg>=3580&sic4_trg<=3580)
	replace trg_indCode48=21 if (sic4_trg>=3581&sic4_trg<=3581)
	replace trg_indCode48=21 if (sic4_trg>=3582&sic4_trg<=3582)
	replace trg_indCode48=21 if (sic4_trg>=3585&sic4_trg<=3585)
	replace trg_indCode48=21 if (sic4_trg>=3586&sic4_trg<=3586)
	replace trg_indCode48=21 if (sic4_trg>=3589&sic4_trg<=3589)
	replace trg_indCode48=21 if (sic4_trg>=3590&sic4_trg<=3599)
	replace trg_indCode48=22 if (sic4_trg>=3600&sic4_trg<=3600)
	replace trg_indCode48=22 if (sic4_trg>=3610&sic4_trg<=3613)
	replace trg_indCode48=22 if (sic4_trg>=3620&sic4_trg<=3621)
	replace trg_indCode48=22 if (sic4_trg>=3623&sic4_trg<=3629)
	replace trg_indCode48=22 if (sic4_trg>=3640&sic4_trg<=3644)
	replace trg_indCode48=22 if (sic4_trg>=3645&sic4_trg<=3645)
	replace trg_indCode48=22 if (sic4_trg>=3646&sic4_trg<=3646)
	replace trg_indCode48=22 if (sic4_trg>=3648&sic4_trg<=3649)
	replace trg_indCode48=22 if (sic4_trg>=3660&sic4_trg<=3660)
	replace trg_indCode48=22 if (sic4_trg>=3690&sic4_trg<=3690)
	replace trg_indCode48=22 if (sic4_trg>=3691&sic4_trg<=3692)
	replace trg_indCode48=22 if (sic4_trg>=3699&sic4_trg<=3699)
	replace trg_indCode48=23 if (sic4_trg>=2296&sic4_trg<=2296)
	replace trg_indCode48=23 if (sic4_trg>=2396&sic4_trg<=2396)
	replace trg_indCode48=23 if (sic4_trg>=3010&sic4_trg<=3011)
	replace trg_indCode48=23 if (sic4_trg>=3537&sic4_trg<=3537)
	replace trg_indCode48=23 if (sic4_trg>=3647&sic4_trg<=3647)
	replace trg_indCode48=23 if (sic4_trg>=3694&sic4_trg<=3694)
	replace trg_indCode48=23 if (sic4_trg>=3700&sic4_trg<=3700)
	replace trg_indCode48=23 if (sic4_trg>=3710&sic4_trg<=3710)
	replace trg_indCode48=23 if (sic4_trg>=3711&sic4_trg<=3711)
	replace trg_indCode48=23 if (sic4_trg>=3713&sic4_trg<=3713)
	replace trg_indCode48=23 if (sic4_trg>=3714&sic4_trg<=3714)
	replace trg_indCode48=23 if (sic4_trg>=3715&sic4_trg<=3715)
	replace trg_indCode48=23 if (sic4_trg>=3716&sic4_trg<=3716)
	replace trg_indCode48=23 if (sic4_trg>=3792&sic4_trg<=3792)
	replace trg_indCode48=23 if (sic4_trg>=3790&sic4_trg<=3791)
	replace trg_indCode48=23 if (sic4_trg>=3799&sic4_trg<=3799)
	replace trg_indCode48=24 if (sic4_trg>=3720&sic4_trg<=3720)
	replace trg_indCode48=24 if (sic4_trg>=3721&sic4_trg<=3721)
	replace trg_indCode48=24 if (sic4_trg>=3723&sic4_trg<=3724)
	replace trg_indCode48=24 if (sic4_trg>=3725&sic4_trg<=3725)
	replace trg_indCode48=24 if (sic4_trg>=3728&sic4_trg<=3729)
	replace trg_indCode48=25 if (sic4_trg>=3730&sic4_trg<=3731)
	replace trg_indCode48=25 if (sic4_trg>=3740&sic4_trg<=3743)
	replace trg_indCode48=26 if (sic4_trg>=3760&sic4_trg<=3769)
	replace trg_indCode48=26 if (sic4_trg>=3795&sic4_trg<=3795)
	replace trg_indCode48=26 if (sic4_trg>=3480&sic4_trg<=3489)
	replace trg_indCode48=27 if (sic4_trg>=1040&sic4_trg<=1049)
	replace trg_indCode48=28 if (sic4_trg>=1000&sic4_trg<=1009)
	replace trg_indCode48=28 if (sic4_trg>=1010&sic4_trg<=1019)
	replace trg_indCode48=28 if (sic4_trg>=1020&sic4_trg<=1029)
	replace trg_indCode48=28 if (sic4_trg>=1030&sic4_trg<=1039)
	replace trg_indCode48=28 if (sic4_trg>=1050&sic4_trg<=1059)
	replace trg_indCode48=28 if (sic4_trg>=1060&sic4_trg<=1069)
	replace trg_indCode48=28 if (sic4_trg>=1070&sic4_trg<=1079)
	replace trg_indCode48=28 if (sic4_trg>=1080&sic4_trg<=1089)
	replace trg_indCode48=28 if (sic4_trg>=1090&sic4_trg<=1099)
	replace trg_indCode48=28 if (sic4_trg>=1100&sic4_trg<=1119)
	replace trg_indCode48=28 if (sic4_trg>=1400&sic4_trg<=1499)
	replace trg_indCode48=29 if (sic4_trg>=1200&sic4_trg<=1299)
	replace trg_indCode48=30 if (sic4_trg>=1300&sic4_trg<=1300)
	replace trg_indCode48=30 if (sic4_trg>=1310&sic4_trg<=1319)
	replace trg_indCode48=30 if (sic4_trg>=1320&sic4_trg<=1329)
	replace trg_indCode48=30 if (sic4_trg>=1330&sic4_trg<=1339)
	replace trg_indCode48=30 if (sic4_trg>=1370&sic4_trg<=1379)
	replace trg_indCode48=30 if (sic4_trg>=1380&sic4_trg<=1380)
	replace trg_indCode48=30 if (sic4_trg>=1381&sic4_trg<=1381)
	replace trg_indCode48=30 if (sic4_trg>=1382&sic4_trg<=1382)
	replace trg_indCode48=30 if (sic4_trg>=1389&sic4_trg<=1389)
	replace trg_indCode48=30 if (sic4_trg>=2900&sic4_trg<=2912)
	replace trg_indCode48=30 if (sic4_trg>=2990&sic4_trg<=2999)
	replace trg_indCode48=31 if (sic4_trg>=4900&sic4_trg<=4900)
	replace trg_indCode48=31 if (sic4_trg>=4910&sic4_trg<=4911)
	replace trg_indCode48=31 if (sic4_trg>=4920&sic4_trg<=4922)
	replace trg_indCode48=31 if (sic4_trg>=4923&sic4_trg<=4923)
	replace trg_indCode48=31 if (sic4_trg>=4924&sic4_trg<=4925)
	replace trg_indCode48=31 if (sic4_trg>=4930&sic4_trg<=4931)
	replace trg_indCode48=31 if (sic4_trg>=4932&sic4_trg<=4932)
	replace trg_indCode48=31 if (sic4_trg>=4939&sic4_trg<=4939)
	replace trg_indCode48=31 if (sic4_trg>=4940&sic4_trg<=4942)
	replace trg_indCode48=32 if (sic4_trg>=4800&sic4_trg<=4800)
	replace trg_indCode48=32 if (sic4_trg>=4810&sic4_trg<=4813)
	replace trg_indCode48=32 if (sic4_trg>=4820&sic4_trg<=4822)
	replace trg_indCode48=32 if (sic4_trg>=4830&sic4_trg<=4839)
	replace trg_indCode48=32 if (sic4_trg>=4840&sic4_trg<=4841)
	replace trg_indCode48=32 if (sic4_trg>=4880&sic4_trg<=4889)
	replace trg_indCode48=32 if (sic4_trg>=4890&sic4_trg<=4890)
	replace trg_indCode48=32 if (sic4_trg>=4891&sic4_trg<=4891)
	replace trg_indCode48=32 if (sic4_trg>=4892&sic4_trg<=4892)
	replace trg_indCode48=32 if (sic4_trg>=4899&sic4_trg<=4899)
	replace trg_indCode48=33 if (sic4_trg>=7020&sic4_trg<=7021)
	replace trg_indCode48=33 if (sic4_trg>=7030&sic4_trg<=7033)
	replace trg_indCode48=33 if (sic4_trg>=7200&sic4_trg<=7200)
	replace trg_indCode48=33 if (sic4_trg>=7210&sic4_trg<=7212)
	replace trg_indCode48=33 if (sic4_trg>=7214&sic4_trg<=7214)
	replace trg_indCode48=33 if (sic4_trg>=7215&sic4_trg<=7216)
	replace trg_indCode48=33 if (sic4_trg>=7217&sic4_trg<=7217)
	replace trg_indCode48=33 if (sic4_trg>=7219&sic4_trg<=7219)
	replace trg_indCode48=33 if (sic4_trg>=7220&sic4_trg<=7221)
	replace trg_indCode48=33 if (sic4_trg>=7230&sic4_trg<=7231)
	replace trg_indCode48=33 if (sic4_trg>=7240&sic4_trg<=7241)
	replace trg_indCode48=33 if (sic4_trg>=7250&sic4_trg<=7251)
	replace trg_indCode48=33 if (sic4_trg>=7260&sic4_trg<=7269)
	replace trg_indCode48=33 if (sic4_trg>=7270&sic4_trg<=7290)
	replace trg_indCode48=33 if (sic4_trg>=7291&sic4_trg<=7291)
	replace trg_indCode48=33 if (sic4_trg>=7292&sic4_trg<=7299)
	replace trg_indCode48=33 if (sic4_trg>=7395&sic4_trg<=7395)
	replace trg_indCode48=33 if (sic4_trg>=7500&sic4_trg<=7500)
	replace trg_indCode48=33 if (sic4_trg>=7520&sic4_trg<=7529)
	replace trg_indCode48=33 if (sic4_trg>=7530&sic4_trg<=7539)
	replace trg_indCode48=33 if (sic4_trg>=7540&sic4_trg<=7549)
	replace trg_indCode48=33 if (sic4_trg>=7600&sic4_trg<=7600)
	replace trg_indCode48=33 if (sic4_trg>=7620&sic4_trg<=7620)
	replace trg_indCode48=33 if (sic4_trg>=7622&sic4_trg<=7622)
	replace trg_indCode48=33 if (sic4_trg>=7623&sic4_trg<=7623)
	replace trg_indCode48=33 if (sic4_trg>=7629&sic4_trg<=7629)
	replace trg_indCode48=33 if (sic4_trg>=7630&sic4_trg<=7631)
	replace trg_indCode48=33 if (sic4_trg>=7640&sic4_trg<=7641)
	replace trg_indCode48=33 if (sic4_trg>=7690&sic4_trg<=7699)
	replace trg_indCode48=33 if (sic4_trg>=8100&sic4_trg<=8199)
	replace trg_indCode48=33 if (sic4_trg>=8200&sic4_trg<=8299)
	replace trg_indCode48=33 if (sic4_trg>=8300&sic4_trg<=8399)
	replace trg_indCode48=33 if (sic4_trg>=8400&sic4_trg<=8499)
	replace trg_indCode48=33 if (sic4_trg>=8600&sic4_trg<=8699)
	replace trg_indCode48=33 if (sic4_trg>=8800&sic4_trg<=8899)
	replace trg_indCode48=33 if (sic4_trg>=7510&sic4_trg<=7515)
	replace trg_indCode48=34 if (sic4_trg>=2750&sic4_trg<=2759)
	replace trg_indCode48=34 if (sic4_trg>=3993&sic4_trg<=3993)
	replace trg_indCode48=34 if (sic4_trg>=7218&sic4_trg<=7218)
	replace trg_indCode48=34 if (sic4_trg>=7300&sic4_trg<=7300)
	replace trg_indCode48=34 if (sic4_trg>=7310&sic4_trg<=7319)
	replace trg_indCode48=34 if (sic4_trg>=7320&sic4_trg<=7329)
	replace trg_indCode48=34 if (sic4_trg>=7330&sic4_trg<=7339)
	replace trg_indCode48=34 if (sic4_trg>=7340&sic4_trg<=7342)
	replace trg_indCode48=34 if (sic4_trg>=7349&sic4_trg<=7349)
	replace trg_indCode48=34 if (sic4_trg>=7350&sic4_trg<=7351)
	replace trg_indCode48=34 if (sic4_trg>=7352&sic4_trg<=7352)
	replace trg_indCode48=34 if (sic4_trg>=7353&sic4_trg<=7353)
	replace trg_indCode48=34 if (sic4_trg>=7359&sic4_trg<=7359)
	replace trg_indCode48=34 if (sic4_trg>=7360&sic4_trg<=7369)
	replace trg_indCode48=34 if (sic4_trg>=7370&sic4_trg<=7372)
	replace trg_indCode48=34 if (sic4_trg>=7374&sic4_trg<=7374)
	replace trg_indCode48=34 if (sic4_trg>=7375&sic4_trg<=7375)
	replace trg_indCode48=34 if (sic4_trg>=7376&sic4_trg<=7376)
	replace trg_indCode48=34 if (sic4_trg>=7377&sic4_trg<=7377)
	replace trg_indCode48=34 if (sic4_trg>=7378&sic4_trg<=7378)
	replace trg_indCode48=34 if (sic4_trg>=7379&sic4_trg<=7379)
	replace trg_indCode48=34 if (sic4_trg>=7380&sic4_trg<=7380)
	replace trg_indCode48=34 if (sic4_trg>=7381&sic4_trg<=7382)
	replace trg_indCode48=34 if (sic4_trg>=7383&sic4_trg<=7383)
	replace trg_indCode48=34 if (sic4_trg>=7384&sic4_trg<=7384)
	replace trg_indCode48=34 if (sic4_trg>=7385&sic4_trg<=7385)
	replace trg_indCode48=34 if (sic4_trg>=7389&sic4_trg<=7390)
	replace trg_indCode48=34 if (sic4_trg>=7391&sic4_trg<=7391)
	replace trg_indCode48=34 if (sic4_trg>=7392&sic4_trg<=7392)
	replace trg_indCode48=34 if (sic4_trg>=7393&sic4_trg<=7393)
	replace trg_indCode48=34 if (sic4_trg>=7394&sic4_trg<=7394)
	replace trg_indCode48=34 if (sic4_trg>=7396&sic4_trg<=7396)
	replace trg_indCode48=34 if (sic4_trg>=7397&sic4_trg<=7397)
	replace trg_indCode48=34 if (sic4_trg>=7399&sic4_trg<=7399)
	replace trg_indCode48=34 if (sic4_trg>=7519&sic4_trg<=7519)
	replace trg_indCode48=34 if (sic4_trg>=8700&sic4_trg<=8700)
	replace trg_indCode48=34 if (sic4_trg>=8710&sic4_trg<=8713)
	replace trg_indCode48=34 if (sic4_trg>=8720&sic4_trg<=8721)
	replace trg_indCode48=34 if (sic4_trg>=8730&sic4_trg<=8734)
	replace trg_indCode48=34 if (sic4_trg>=8740&sic4_trg<=8748)
	replace trg_indCode48=34 if (sic4_trg>=8900&sic4_trg<=8910)
	replace trg_indCode48=34 if (sic4_trg>=8911&sic4_trg<=8911)
	replace trg_indCode48=34 if (sic4_trg>=8920&sic4_trg<=8999)
	replace trg_indCode48=34 if (sic4_trg>=4220&sic4_trg<=4229)
	replace trg_indCode48=35 if (sic4_trg>=3570&sic4_trg<=3579)
	replace trg_indCode48=35 if (sic4_trg>=3680&sic4_trg<=3680)
	replace trg_indCode48=35 if (sic4_trg>=3681&sic4_trg<=3681)
	replace trg_indCode48=35 if (sic4_trg>=3682&sic4_trg<=3682)
	replace trg_indCode48=35 if (sic4_trg>=3683&sic4_trg<=3683)
	replace trg_indCode48=35 if (sic4_trg>=3684&sic4_trg<=3684)
	replace trg_indCode48=35 if (sic4_trg>=3685&sic4_trg<=3685)
	replace trg_indCode48=35 if (sic4_trg>=3686&sic4_trg<=3686)
	replace trg_indCode48=35 if (sic4_trg>=3687&sic4_trg<=3687)
	replace trg_indCode48=35 if (sic4_trg>=3688&sic4_trg<=3688)
	replace trg_indCode48=35 if (sic4_trg>=3689&sic4_trg<=3689)
	replace trg_indCode48=35 if (sic4_trg>=3695&sic4_trg<=3695)
	replace trg_indCode48=35 if (sic4_trg>=7373&sic4_trg<=7373)
	replace trg_indCode48=36 if (sic4_trg>=3622&sic4_trg<=3622)
	replace trg_indCode48=36 if (sic4_trg>=3661&sic4_trg<=3661)
	replace trg_indCode48=36 if (sic4_trg>=3662&sic4_trg<=3662)
	replace trg_indCode48=36 if (sic4_trg>=3663&sic4_trg<=3663)
	replace trg_indCode48=36 if (sic4_trg>=3664&sic4_trg<=3664)
	replace trg_indCode48=36 if (sic4_trg>=3665&sic4_trg<=3665)
	replace trg_indCode48=36 if (sic4_trg>=3666&sic4_trg<=3666)
	replace trg_indCode48=36 if (sic4_trg>=3669&sic4_trg<=3669)
	replace trg_indCode48=36 if (sic4_trg>=3670&sic4_trg<=3679)
	replace trg_indCode48=36 if (sic4_trg>=3810&sic4_trg<=3810)
	replace trg_indCode48=36 if (sic4_trg>=3812&sic4_trg<=3812)
	replace trg_indCode48=37 if (sic4_trg>=3811&sic4_trg<=3811)
	replace trg_indCode48=37 if (sic4_trg>=3820&sic4_trg<=3820)
	replace trg_indCode48=37 if (sic4_trg>=3821&sic4_trg<=3821)
	replace trg_indCode48=37 if (sic4_trg>=3822&sic4_trg<=3822)
	replace trg_indCode48=37 if (sic4_trg>=3823&sic4_trg<=3823)
	replace trg_indCode48=37 if (sic4_trg>=3824&sic4_trg<=3824)
	replace trg_indCode48=37 if (sic4_trg>=3825&sic4_trg<=3825)
	replace trg_indCode48=37 if (sic4_trg>=3826&sic4_trg<=3826)
	replace trg_indCode48=37 if (sic4_trg>=3827&sic4_trg<=3827)
	replace trg_indCode48=37 if (sic4_trg>=3829&sic4_trg<=3829)
	replace trg_indCode48=37 if (sic4_trg>=3830&sic4_trg<=3839)
	replace trg_indCode48=38 if (sic4_trg>=2520&sic4_trg<=2549)
	replace trg_indCode48=38 if (sic4_trg>=2600&sic4_trg<=2639)
	replace trg_indCode48=38 if (sic4_trg>=2670&sic4_trg<=2699)
	replace trg_indCode48=38 if (sic4_trg>=2760&sic4_trg<=2761)
	replace trg_indCode48=38 if (sic4_trg>=3950&sic4_trg<=3955)
	replace trg_indCode48=39 if (sic4_trg>=2440&sic4_trg<=2449)
	replace trg_indCode48=39 if (sic4_trg>=2640&sic4_trg<=2659)
	replace trg_indCode48=39 if (sic4_trg>=3220&sic4_trg<=3221)
	replace trg_indCode48=39 if (sic4_trg>=3410&sic4_trg<=3412)
	replace trg_indCode48=40 if (sic4_trg>=4000&sic4_trg<=4013)
	replace trg_indCode48=40 if (sic4_trg>=4040&sic4_trg<=4049)
	replace trg_indCode48=40 if (sic4_trg>=4100&sic4_trg<=4100)
	replace trg_indCode48=40 if (sic4_trg>=4110&sic4_trg<=4119)
	replace trg_indCode48=40 if (sic4_trg>=4120&sic4_trg<=4121)
	replace trg_indCode48=40 if (sic4_trg>=4130&sic4_trg<=4131)
	replace trg_indCode48=40 if (sic4_trg>=4140&sic4_trg<=4142)
	replace trg_indCode48=40 if (sic4_trg>=4150&sic4_trg<=4151)
	replace trg_indCode48=40 if (sic4_trg>=4170&sic4_trg<=4173)
	replace trg_indCode48=40 if (sic4_trg>=4190&sic4_trg<=4199)
	replace trg_indCode48=40 if (sic4_trg>=4200&sic4_trg<=4200)
	replace trg_indCode48=40 if (sic4_trg>=4210&sic4_trg<=4219)
	replace trg_indCode48=40 if (sic4_trg>=4230&sic4_trg<=4231)
	replace trg_indCode48=40 if (sic4_trg>=4240&sic4_trg<=4249)
	replace trg_indCode48=40 if (sic4_trg>=4400&sic4_trg<=4499)
	replace trg_indCode48=40 if (sic4_trg>=4500&sic4_trg<=4599)
	replace trg_indCode48=40 if (sic4_trg>=4600&sic4_trg<=4699)
	replace trg_indCode48=40 if (sic4_trg>=4700&sic4_trg<=4700)
	replace trg_indCode48=40 if (sic4_trg>=4710&sic4_trg<=4712)
	replace trg_indCode48=40 if (sic4_trg>=4720&sic4_trg<=4729)
	replace trg_indCode48=40 if (sic4_trg>=4730&sic4_trg<=4739)
	replace trg_indCode48=40 if (sic4_trg>=4740&sic4_trg<=4749)
	replace trg_indCode48=40 if (sic4_trg>=4780&sic4_trg<=4780)
	replace trg_indCode48=40 if (sic4_trg>=4782&sic4_trg<=4782)
	replace trg_indCode48=40 if (sic4_trg>=4783&sic4_trg<=4783)
	replace trg_indCode48=40 if (sic4_trg>=4784&sic4_trg<=4784)
	replace trg_indCode48=40 if (sic4_trg>=4785&sic4_trg<=4785)
	replace trg_indCode48=40 if (sic4_trg>=4789&sic4_trg<=4789)
	replace trg_indCode48=41 if (sic4_trg>=5000&sic4_trg<=5000)
	replace trg_indCode48=41 if (sic4_trg>=5010&sic4_trg<=5015)
	replace trg_indCode48=41 if (sic4_trg>=5020&sic4_trg<=5023)
	replace trg_indCode48=41 if (sic4_trg>=5030&sic4_trg<=5039)
	replace trg_indCode48=41 if (sic4_trg>=5040&sic4_trg<=5042)
	replace trg_indCode48=41 if (sic4_trg>=5043&sic4_trg<=5043)
	replace trg_indCode48=41 if (sic4_trg>=5044&sic4_trg<=5044)
	replace trg_indCode48=41 if (sic4_trg>=5045&sic4_trg<=5045)
	replace trg_indCode48=41 if (sic4_trg>=5046&sic4_trg<=5046)
	replace trg_indCode48=41 if (sic4_trg>=5047&sic4_trg<=5047)
	replace trg_indCode48=41 if (sic4_trg>=5048&sic4_trg<=5048)
	replace trg_indCode48=41 if (sic4_trg>=5049&sic4_trg<=5049)
	replace trg_indCode48=41 if (sic4_trg>=5050&sic4_trg<=5059)
	replace trg_indCode48=41 if (sic4_trg>=5060&sic4_trg<=5060)
	replace trg_indCode48=41 if (sic4_trg>=5063&sic4_trg<=5063)
	replace trg_indCode48=41 if (sic4_trg>=5064&sic4_trg<=5064)
	replace trg_indCode48=41 if (sic4_trg>=5065&sic4_trg<=5065)
	replace trg_indCode48=41 if (sic4_trg>=5070&sic4_trg<=5078)
	replace trg_indCode48=41 if (sic4_trg>=5080&sic4_trg<=5080)
	replace trg_indCode48=41 if (sic4_trg>=5081&sic4_trg<=5081)
	replace trg_indCode48=41 if (sic4_trg>=5082&sic4_trg<=5082)
	replace trg_indCode48=41 if (sic4_trg>=5083&sic4_trg<=5083)
	replace trg_indCode48=41 if (sic4_trg>=5084&sic4_trg<=5084)
	replace trg_indCode48=41 if (sic4_trg>=5085&sic4_trg<=5085)
	replace trg_indCode48=41 if (sic4_trg>=5086&sic4_trg<=5087)
	replace trg_indCode48=41 if (sic4_trg>=5088&sic4_trg<=5088)
	replace trg_indCode48=41 if (sic4_trg>=5090&sic4_trg<=5090)
	replace trg_indCode48=41 if (sic4_trg>=5091&sic4_trg<=5092)
	replace trg_indCode48=41 if (sic4_trg>=5093&sic4_trg<=5093)
	replace trg_indCode48=41 if (sic4_trg>=5094&sic4_trg<=5094)
	replace trg_indCode48=41 if (sic4_trg>=5099&sic4_trg<=5099)
	replace trg_indCode48=41 if (sic4_trg>=5100&sic4_trg<=5100)
	replace trg_indCode48=41 if (sic4_trg>=5110&sic4_trg<=5113)
	replace trg_indCode48=41 if (sic4_trg>=5120&sic4_trg<=5122)
	replace trg_indCode48=41 if (sic4_trg>=5130&sic4_trg<=5139)
	replace trg_indCode48=41 if (sic4_trg>=5140&sic4_trg<=5149)
	replace trg_indCode48=41 if (sic4_trg>=5150&sic4_trg<=5159)
	replace trg_indCode48=41 if (sic4_trg>=5160&sic4_trg<=5169)
	replace trg_indCode48=41 if (sic4_trg>=5170&sic4_trg<=5172)
	replace trg_indCode48=41 if (sic4_trg>=5180&sic4_trg<=5182)
	replace trg_indCode48=41 if (sic4_trg>=5190&sic4_trg<=5199)
	replace trg_indCode48=42 if (sic4_trg>=5200&sic4_trg<=5200)
	replace trg_indCode48=42 if (sic4_trg>=5210&sic4_trg<=5219)
	replace trg_indCode48=42 if (sic4_trg>=5220&sic4_trg<=5229)
	replace trg_indCode48=42 if (sic4_trg>=5230&sic4_trg<=5231)
	replace trg_indCode48=42 if (sic4_trg>=5250&sic4_trg<=5251)
	replace trg_indCode48=42 if (sic4_trg>=5260&sic4_trg<=5261)
	replace trg_indCode48=42 if (sic4_trg>=5270&sic4_trg<=5271)
	replace trg_indCode48=42 if (sic4_trg>=5300&sic4_trg<=5300)
	replace trg_indCode48=42 if (sic4_trg>=5310&sic4_trg<=5311)
	replace trg_indCode48=42 if (sic4_trg>=5320&sic4_trg<=5320)
	replace trg_indCode48=42 if (sic4_trg>=5330&sic4_trg<=5331)
	replace trg_indCode48=42 if (sic4_trg>=5334&sic4_trg<=5334)
	replace trg_indCode48=42 if (sic4_trg>=5340&sic4_trg<=5349)
	replace trg_indCode48=42 if (sic4_trg>=5390&sic4_trg<=5399)
	replace trg_indCode48=42 if (sic4_trg>=5400&sic4_trg<=5400)
	replace trg_indCode48=42 if (sic4_trg>=5410&sic4_trg<=5411)
	replace trg_indCode48=42 if (sic4_trg>=5412&sic4_trg<=5412)
	replace trg_indCode48=42 if (sic4_trg>=5420&sic4_trg<=5429)
	replace trg_indCode48=42 if (sic4_trg>=5430&sic4_trg<=5439)
	replace trg_indCode48=42 if (sic4_trg>=5440&sic4_trg<=5449)
	replace trg_indCode48=42 if (sic4_trg>=5450&sic4_trg<=5459)
	replace trg_indCode48=42 if (sic4_trg>=5460&sic4_trg<=5469)
	replace trg_indCode48=42 if (sic4_trg>=5490&sic4_trg<=5499)
	replace trg_indCode48=42 if (sic4_trg>=5500&sic4_trg<=5500)
	replace trg_indCode48=42 if (sic4_trg>=5510&sic4_trg<=5529)
	replace trg_indCode48=42 if (sic4_trg>=5530&sic4_trg<=5539)
	replace trg_indCode48=42 if (sic4_trg>=5540&sic4_trg<=5549)
	replace trg_indCode48=42 if (sic4_trg>=5550&sic4_trg<=5559)
	replace trg_indCode48=42 if (sic4_trg>=5560&sic4_trg<=5569)
	replace trg_indCode48=42 if (sic4_trg>=5570&sic4_trg<=5579)
	replace trg_indCode48=42 if (sic4_trg>=5590&sic4_trg<=5599)
	replace trg_indCode48=42 if (sic4_trg>=5600&sic4_trg<=5699)
	replace trg_indCode48=42 if (sic4_trg>=5700&sic4_trg<=5700)
	replace trg_indCode48=42 if (sic4_trg>=5710&sic4_trg<=5719)
	replace trg_indCode48=42 if (sic4_trg>=5720&sic4_trg<=5722)
	replace trg_indCode48=42 if (sic4_trg>=5730&sic4_trg<=5733)
	replace trg_indCode48=42 if (sic4_trg>=5734&sic4_trg<=5734)
	replace trg_indCode48=42 if (sic4_trg>=5735&sic4_trg<=5735)
	replace trg_indCode48=42 if (sic4_trg>=5736&sic4_trg<=5736)
	replace trg_indCode48=42 if (sic4_trg>=5750&sic4_trg<=5799)
	replace trg_indCode48=42 if (sic4_trg>=5900&sic4_trg<=5900)
	replace trg_indCode48=42 if (sic4_trg>=5910&sic4_trg<=5912)
	replace trg_indCode48=42 if (sic4_trg>=5920&sic4_trg<=5929)
	replace trg_indCode48=42 if (sic4_trg>=5930&sic4_trg<=5932)
	replace trg_indCode48=42 if (sic4_trg>=5940&sic4_trg<=5940)
	replace trg_indCode48=42 if (sic4_trg>=5941&sic4_trg<=5941)
	replace trg_indCode48=42 if (sic4_trg>=5942&sic4_trg<=5942)
	replace trg_indCode48=42 if (sic4_trg>=5943&sic4_trg<=5943)
	replace trg_indCode48=42 if (sic4_trg>=5944&sic4_trg<=5944)
	replace trg_indCode48=42 if (sic4_trg>=5945&sic4_trg<=5945)
	replace trg_indCode48=42 if (sic4_trg>=5946&sic4_trg<=5946)
	replace trg_indCode48=42 if (sic4_trg>=5947&sic4_trg<=5947)
	replace trg_indCode48=42 if (sic4_trg>=5948&sic4_trg<=5948)
	replace trg_indCode48=42 if (sic4_trg>=5949&sic4_trg<=5949)
	replace trg_indCode48=42 if (sic4_trg>=5950&sic4_trg<=5959)
	replace trg_indCode48=42 if (sic4_trg>=5960&sic4_trg<=5969)
	replace trg_indCode48=42 if (sic4_trg>=5970&sic4_trg<=5979)
	replace trg_indCode48=42 if (sic4_trg>=5980&sic4_trg<=5989)
	replace trg_indCode48=42 if (sic4_trg>=5990&sic4_trg<=5990)
	replace trg_indCode48=42 if (sic4_trg>=5992&sic4_trg<=5992)
	replace trg_indCode48=42 if (sic4_trg>=5993&sic4_trg<=5993)
	replace trg_indCode48=42 if (sic4_trg>=5994&sic4_trg<=5994)
	replace trg_indCode48=42 if (sic4_trg>=5995&sic4_trg<=5995)
	replace trg_indCode48=42 if (sic4_trg>=5999&sic4_trg<=5999)
	replace trg_indCode48=43 if (sic4_trg>=5800&sic4_trg<=5819)
	replace trg_indCode48=43 if (sic4_trg>=5820&sic4_trg<=5829)
	replace trg_indCode48=43 if (sic4_trg>=5890&sic4_trg<=5899)
	replace trg_indCode48=43 if (sic4_trg>=7000&sic4_trg<=7000)
	replace trg_indCode48=43 if (sic4_trg>=7010&sic4_trg<=7019)
	replace trg_indCode48=43 if (sic4_trg>=7040&sic4_trg<=7049)
	replace trg_indCode48=43 if (sic4_trg>=7213&sic4_trg<=7213)
	replace trg_indCode48=44 if (sic4_trg>=6000&sic4_trg<=6000)
	replace trg_indCode48=44 if (sic4_trg>=6010&sic4_trg<=6019)
	replace trg_indCode48=44 if (sic4_trg>=6020&sic4_trg<=6020)
	replace trg_indCode48=44 if (sic4_trg>=6021&sic4_trg<=6021)
	replace trg_indCode48=44 if (sic4_trg>=6022&sic4_trg<=6022)
	replace trg_indCode48=44 if (sic4_trg>=6023&sic4_trg<=6024)
	replace trg_indCode48=44 if (sic4_trg>=6025&sic4_trg<=6025)
	replace trg_indCode48=44 if (sic4_trg>=6026&sic4_trg<=6026)
	replace trg_indCode48=44 if (sic4_trg>=6027&sic4_trg<=6027)
	replace trg_indCode48=44 if (sic4_trg>=6028&sic4_trg<=6029)
	replace trg_indCode48=44 if (sic4_trg>=6030&sic4_trg<=6036)
	replace trg_indCode48=44 if (sic4_trg>=6040&sic4_trg<=6059)
	replace trg_indCode48=44 if (sic4_trg>=6060&sic4_trg<=6062)
	replace trg_indCode48=44 if (sic4_trg>=6080&sic4_trg<=6082)
	replace trg_indCode48=44 if (sic4_trg>=6090&sic4_trg<=6099)
	replace trg_indCode48=44 if (sic4_trg>=6100&sic4_trg<=6100)
	replace trg_indCode48=44 if (sic4_trg>=6110&sic4_trg<=6111)
	replace trg_indCode48=44 if (sic4_trg>=6112&sic4_trg<=6113)
	replace trg_indCode48=44 if (sic4_trg>=6120&sic4_trg<=6129)
	replace trg_indCode48=44 if (sic4_trg>=6130&sic4_trg<=6139)
	replace trg_indCode48=44 if (sic4_trg>=6140&sic4_trg<=6149)
	replace trg_indCode48=44 if (sic4_trg>=6150&sic4_trg<=6159)
	replace trg_indCode48=44 if (sic4_trg>=6160&sic4_trg<=6169)
	replace trg_indCode48=44 if (sic4_trg>=6170&sic4_trg<=6179)
	replace trg_indCode48=44 if (sic4_trg>=6190&sic4_trg<=6199)
	replace trg_indCode48=45 if (sic4_trg>=6300&sic4_trg<=6300)
	replace trg_indCode48=45 if (sic4_trg>=6310&sic4_trg<=6319)
	replace trg_indCode48=45 if (sic4_trg>=6320&sic4_trg<=6329)
	replace trg_indCode48=45 if (sic4_trg>=6330&sic4_trg<=6331)
	replace trg_indCode48=45 if (sic4_trg>=6350&sic4_trg<=6351)
	replace trg_indCode48=45 if (sic4_trg>=6360&sic4_trg<=6361)
	replace trg_indCode48=45 if (sic4_trg>=6370&sic4_trg<=6379)
	replace trg_indCode48=45 if (sic4_trg>=6390&sic4_trg<=6399)
	replace trg_indCode48=45 if (sic4_trg>=6400&sic4_trg<=6411)
	replace trg_indCode48=46 if (sic4_trg>=6500&sic4_trg<=6500)
	replace trg_indCode48=46 if (sic4_trg>=6510&sic4_trg<=6510)
	replace trg_indCode48=46 if (sic4_trg>=6512&sic4_trg<=6512)
	replace trg_indCode48=46 if (sic4_trg>=6513&sic4_trg<=6513)
	replace trg_indCode48=46 if (sic4_trg>=6514&sic4_trg<=6514)
	replace trg_indCode48=46 if (sic4_trg>=6515&sic4_trg<=6515)
	replace trg_indCode48=46 if (sic4_trg>=6517&sic4_trg<=6519)
	replace trg_indCode48=46 if (sic4_trg>=6520&sic4_trg<=6529)
	replace trg_indCode48=46 if (sic4_trg>=6530&sic4_trg<=6531)
	replace trg_indCode48=46 if (sic4_trg>=6532&sic4_trg<=6532)
	replace trg_indCode48=46 if (sic4_trg>=6540&sic4_trg<=6541)
	replace trg_indCode48=46 if (sic4_trg>=6550&sic4_trg<=6553)
	replace trg_indCode48=46 if (sic4_trg>=6590&sic4_trg<=6599)
	replace trg_indCode48=46 if (sic4_trg>=6610&sic4_trg<=6611)
	replace trg_indCode48=47 if (sic4_trg>=6200&sic4_trg<=6299)
	replace trg_indCode48=47 if (sic4_trg>=6700&sic4_trg<=6700)
	replace trg_indCode48=47 if (sic4_trg>=6710&sic4_trg<=6719)
	replace trg_indCode48=47 if (sic4_trg>=6720&sic4_trg<=6722)
	replace trg_indCode48=47 if (sic4_trg>=6723&sic4_trg<=6723)
	replace trg_indCode48=47 if (sic4_trg>=6724&sic4_trg<=6724)
	replace trg_indCode48=47 if (sic4_trg>=6725&sic4_trg<=6725)
	replace trg_indCode48=47 if (sic4_trg>=6726&sic4_trg<=6726)
	replace trg_indCode48=47 if (sic4_trg>=6730&sic4_trg<=6733)
	replace trg_indCode48=47 if (sic4_trg>=6740&sic4_trg<=6779)
	replace trg_indCode48=47 if (sic4_trg>=6790&sic4_trg<=6791)
	replace trg_indCode48=47 if (sic4_trg>=6792&sic4_trg<=6792)
	replace trg_indCode48=47 if (sic4_trg>=6793&sic4_trg<=6793)
	replace trg_indCode48=47 if (sic4_trg>=6794&sic4_trg<=6794)
	replace trg_indCode48=47 if (sic4_trg>=6795&sic4_trg<=6795)
	replace trg_indCode48=47 if (sic4_trg>=6798&sic4_trg<=6798)
	replace trg_indCode48=47 if (sic4_trg>=6799&sic4_trg<=6799)
	replace trg_indCode48=48 if (sic4_trg>=4950&sic4_trg<=4959)
	replace trg_indCode48=48 if (sic4_trg>=4960&sic4_trg<=4961)
	replace trg_indCode48=48 if (sic4_trg>=4970&sic4_trg<=4971)
	replace trg_indCode48=48 if (sic4_trg>=4990&sic4_trg<=4991)
	label define trg_indCode48Names 1 "Agriculture" 2 "Food Products" 3 "Candy & Soda" 4 "Beer & Liquor" 5 "Tobacco Products" 6 "Recreation" 7 "Entertainment" 8 "Printing and Publishing" 9 "Consumer Goods" 10 "Apparel" 11 "Healthcare" 12 "Medical Equipment" 13 "Pharmaceutical Products" 14 "Chemicals" 15 "Rubber and Plastic Products" 16 "Textiles" 17 "Construction Materials" 18 "Construction" 19 "Steel Works Etc" 20 "Fabricated Products" 21 "Machinery" 22 "Electrical Equipment" 23 "Automobiles and Trucks" 24 "Aircraft" 25 "Shipbuilding, Railroad Equipment" 26 "Defense" 27 "Precious Metals" 28 "Non-Metallic and Industrial Metal Mining" 29 "Coal" 30 "Petroleum and Natural Gas" 31 "Utilities" 32 "Communication" 33 "Personal Services" 34 "Business Services" 35 "Computers" 36 "Electronic Equipment" 37 "Measuring and Control Equipment" 38 "Business Supplies" 39 "Shipping Containers" 40 "Transportation" 41 "Wholesale" 42 "Retail" 43 "Restaraunts, Hotels, Motels" 44 "Banking" 45 "Insurance" 46 "Real Estate" 47 "Trading" 48 "Almost Nothing"
	label values trg_indCode48 trg_indCode48Names
	
	
	* Acquirer Industry classification
	gen sic4_acq = real(acqprisic)
	* French 12 industries
	gen acq_indCode=12	
	replace acq_indCode=1 if (sic4_acq>=100&sic4_acq<=999)
	replace acq_indCode=1 if (sic4_acq>=2000&sic4_acq<=2399)
	replace acq_indCode=1 if (sic4_acq>=2700&sic4_acq<=2749)
	replace acq_indCode=1 if (sic4_acq>=2770&sic4_acq<=2799)
	replace acq_indCode=1 if (sic4_acq>=3100&sic4_acq<=3199)
	replace acq_indCode=1 if (sic4_acq>=3940&sic4_acq<=3989)
	replace acq_indCode=2 if (sic4_acq>=2500&sic4_acq<=2519)
	replace acq_indCode=2 if (sic4_acq>=2590&sic4_acq<=2599)
	replace acq_indCode=2 if (sic4_acq>=3630&sic4_acq<=3659)
	replace acq_indCode=2 if (sic4_acq>=3710&sic4_acq<=3711)
	replace acq_indCode=2 if (sic4_acq>=3714&sic4_acq<=3714)
	replace acq_indCode=2 if (sic4_acq>=3716&sic4_acq<=3716)
	replace acq_indCode=2 if (sic4_acq>=3750&sic4_acq<=3751)
	replace acq_indCode=2 if (sic4_acq>=3792&sic4_acq<=3792)
	replace acq_indCode=2 if (sic4_acq>=3900&sic4_acq<=3939)
	replace acq_indCode=2 if (sic4_acq>=3990&sic4_acq<=3999)
	replace acq_indCode=3 if (sic4_acq>=2520&sic4_acq<=2589)
	replace acq_indCode=3 if (sic4_acq>=2600&sic4_acq<=2699)
	replace acq_indCode=3 if (sic4_acq>=2750&sic4_acq<=2769)
	replace acq_indCode=3 if (sic4_acq>=3000&sic4_acq<=3099)
	replace acq_indCode=3 if (sic4_acq>=3200&sic4_acq<=3569)
	replace acq_indCode=3 if (sic4_acq>=3580&sic4_acq<=3629)
	replace acq_indCode=3 if (sic4_acq>=3700&sic4_acq<=3709)
	replace acq_indCode=3 if (sic4_acq>=3712&sic4_acq<=3713)
	replace acq_indCode=3 if (sic4_acq>=3715&sic4_acq<=3715)
	replace acq_indCode=3 if (sic4_acq>=3717&sic4_acq<=3749)
	replace acq_indCode=3 if (sic4_acq>=3752&sic4_acq<=3791)
	replace acq_indCode=3 if (sic4_acq>=3793&sic4_acq<=3799)
	replace acq_indCode=3 if (sic4_acq>=3830&sic4_acq<=3839)
	replace acq_indCode=3 if (sic4_acq>=3860&sic4_acq<=3899)
	replace acq_indCode=4 if (sic4_acq>=1200&sic4_acq<=1399)
	replace acq_indCode=4 if (sic4_acq>=2900&sic4_acq<=2999)
	replace acq_indCode=5 if (sic4_acq>=2800&sic4_acq<=2829)
	replace acq_indCode=5 if (sic4_acq>=2840&sic4_acq<=2899)
	replace acq_indCode=6 if (sic4_acq>=3570&sic4_acq<=3579)
	replace acq_indCode=6 if (sic4_acq>=3660&sic4_acq<=3692)
	replace acq_indCode=6 if (sic4_acq>=3694&sic4_acq<=3699)
	replace acq_indCode=6 if (sic4_acq>=3810&sic4_acq<=3829)
	replace acq_indCode=6 if (sic4_acq>=7370&sic4_acq<=7379)
	replace acq_indCode=7 if (sic4_acq>=4800&sic4_acq<=4899)
	replace acq_indCode=8 if (sic4_acq>=4900&sic4_acq<=4949)
	replace acq_indCode=9 if (sic4_acq>=5000&sic4_acq<=5999)
	replace acq_indCode=9 if (sic4_acq>=7200&sic4_acq<=7299)
	replace acq_indCode=9 if (sic4_acq>=7600&sic4_acq<=7699)
	replace acq_indCode=10 if (sic4_acq>=2830&sic4_acq<=2839)
	replace acq_indCode=10 if (sic4_acq>=3693&sic4_acq<=3693)
	replace acq_indCode=10 if (sic4_acq>=3840&sic4_acq<=3859)
	replace acq_indCode=10 if (sic4_acq>=8000&sic4_acq<=8099)
	replace acq_indCode=11 if (sic4_acq>=6000&sic4_acq<=6999)
	label define acq_indNames 1 "Non Durables" 2 "Durables" 3 "Manufacturing" 4 "Energy" 5 "Chemicals" 6 "Business Equipment" 7 "Telecoms" 8 "Utilities" 9 "Wholesales" 10 "Healthcare" 11 "Finance" 12 "Others"
	label values acq_indCode indNames
	
	* French 48 industries
	gen acq_indCode48=48	
	replace acq_indCode48=1 if (sic4_acq>=100&sic4_acq<=199)
	replace acq_indCode48=1 if (sic4_acq>=200&sic4_acq<=299)
	replace acq_indCode48=1 if (sic4_acq>=700&sic4_acq<=799)
	replace acq_indCode48=1 if (sic4_acq>=910&sic4_acq<=919)
	replace acq_indCode48=1 if (sic4_acq>=2048&sic4_acq<=2048)
	replace acq_indCode48=2 if (sic4_acq>=2000&sic4_acq<=2009)
	replace acq_indCode48=2 if (sic4_acq>=2010&sic4_acq<=2019)
	replace acq_indCode48=2 if (sic4_acq>=2020&sic4_acq<=2029)
	replace acq_indCode48=2 if (sic4_acq>=2030&sic4_acq<=2039)
	replace acq_indCode48=2 if (sic4_acq>=2040&sic4_acq<=2046)
	replace acq_indCode48=2 if (sic4_acq>=2050&sic4_acq<=2059)
	replace acq_indCode48=2 if (sic4_acq>=2060&sic4_acq<=2063)
	replace acq_indCode48=2 if (sic4_acq>=2070&sic4_acq<=2079)
	replace acq_indCode48=2 if (sic4_acq>=2090&sic4_acq<=2092)
	replace acq_indCode48=2 if (sic4_acq>=2095&sic4_acq<=2095)
	replace acq_indCode48=2 if (sic4_acq>=2098&sic4_acq<=2099)
	replace acq_indCode48=3 if (sic4_acq>=2064&sic4_acq<=2068)
	replace acq_indCode48=3 if (sic4_acq>=2086&sic4_acq<=2086)
	replace acq_indCode48=3 if (sic4_acq>=2087&sic4_acq<=2087)
	replace acq_indCode48=3 if (sic4_acq>=2096&sic4_acq<=2096)
	replace acq_indCode48=3 if (sic4_acq>=2097&sic4_acq<=2097)
	replace acq_indCode48=4 if (sic4_acq>=2080&sic4_acq<=2080)
	replace acq_indCode48=4 if (sic4_acq>=2082&sic4_acq<=2082)
	replace acq_indCode48=4 if (sic4_acq>=2083&sic4_acq<=2083)
	replace acq_indCode48=4 if (sic4_acq>=2084&sic4_acq<=2084)
	replace acq_indCode48=4 if (sic4_acq>=2085&sic4_acq<=2085)
	replace acq_indCode48=5 if (sic4_acq>=2100&sic4_acq<=2199)
	replace acq_indCode48=6 if (sic4_acq>=920&sic4_acq<=999)
	replace acq_indCode48=6 if (sic4_acq>=3650&sic4_acq<=3651)
	replace acq_indCode48=6 if (sic4_acq>=3652&sic4_acq<=3652)
	replace acq_indCode48=6 if (sic4_acq>=3732&sic4_acq<=3732)
	replace acq_indCode48=6 if (sic4_acq>=3930&sic4_acq<=3931)
	replace acq_indCode48=6 if (sic4_acq>=3940&sic4_acq<=3949)
	replace acq_indCode48=7 if (sic4_acq>=7800&sic4_acq<=7829)
	replace acq_indCode48=7 if (sic4_acq>=7830&sic4_acq<=7833)
	replace acq_indCode48=7 if (sic4_acq>=7840&sic4_acq<=7841)
	replace acq_indCode48=7 if (sic4_acq>=7900&sic4_acq<=7900)
	replace acq_indCode48=7 if (sic4_acq>=7910&sic4_acq<=7911)
	replace acq_indCode48=7 if (sic4_acq>=7920&sic4_acq<=7929)
	replace acq_indCode48=7 if (sic4_acq>=7930&sic4_acq<=7933)
	replace acq_indCode48=7 if (sic4_acq>=7940&sic4_acq<=7949)
	replace acq_indCode48=7 if (sic4_acq>=7980&sic4_acq<=7980)
	replace acq_indCode48=7 if (sic4_acq>=7990&sic4_acq<=7999)
	replace acq_indCode48=8 if (sic4_acq>=2700&sic4_acq<=2709)
	replace acq_indCode48=8 if (sic4_acq>=2710&sic4_acq<=2719)
	replace acq_indCode48=8 if (sic4_acq>=2720&sic4_acq<=2729)
	replace acq_indCode48=8 if (sic4_acq>=2730&sic4_acq<=2739)
	replace acq_indCode48=8 if (sic4_acq>=2740&sic4_acq<=2749)
	replace acq_indCode48=8 if (sic4_acq>=2770&sic4_acq<=2771)
	replace acq_indCode48=8 if (sic4_acq>=2780&sic4_acq<=2789)
	replace acq_indCode48=8 if (sic4_acq>=2790&sic4_acq<=2799)
	replace acq_indCode48=9 if (sic4_acq>=2047&sic4_acq<=2047)
	replace acq_indCode48=9 if (sic4_acq>=2391&sic4_acq<=2392)
	replace acq_indCode48=9 if (sic4_acq>=2510&sic4_acq<=2519)
	replace acq_indCode48=9 if (sic4_acq>=2590&sic4_acq<=2599)
	replace acq_indCode48=9 if (sic4_acq>=2840&sic4_acq<=2843)
	replace acq_indCode48=9 if (sic4_acq>=2844&sic4_acq<=2844)
	replace acq_indCode48=9 if (sic4_acq>=3160&sic4_acq<=3161)
	replace acq_indCode48=9 if (sic4_acq>=3170&sic4_acq<=3171)
	replace acq_indCode48=9 if (sic4_acq>=3172&sic4_acq<=3172)
	replace acq_indCode48=9 if (sic4_acq>=3190&sic4_acq<=3199)
	replace acq_indCode48=9 if (sic4_acq>=3229&sic4_acq<=3229)
	replace acq_indCode48=9 if (sic4_acq>=3260&sic4_acq<=3260)
	replace acq_indCode48=9 if (sic4_acq>=3262&sic4_acq<=3263)
	replace acq_indCode48=9 if (sic4_acq>=3269&sic4_acq<=3269)
	replace acq_indCode48=9 if (sic4_acq>=3230&sic4_acq<=3231)
	replace acq_indCode48=9 if (sic4_acq>=3630&sic4_acq<=3639)
	replace acq_indCode48=9 if (sic4_acq>=3750&sic4_acq<=3751)
	replace acq_indCode48=9 if (sic4_acq>=3800&sic4_acq<=3800)
	replace acq_indCode48=9 if (sic4_acq>=3860&sic4_acq<=3861)
	replace acq_indCode48=9 if (sic4_acq>=3870&sic4_acq<=3873)
	replace acq_indCode48=9 if (sic4_acq>=3910&sic4_acq<=3911)
	replace acq_indCode48=9 if (sic4_acq>=3914&sic4_acq<=3914)
	replace acq_indCode48=9 if (sic4_acq>=3915&sic4_acq<=3915)
	replace acq_indCode48=9 if (sic4_acq>=3960&sic4_acq<=3962)
	replace acq_indCode48=9 if (sic4_acq>=3991&sic4_acq<=3991)
	replace acq_indCode48=9 if (sic4_acq>=3995&sic4_acq<=3995)
	replace acq_indCode48=10 if (sic4_acq>=2300&sic4_acq<=2390)
	replace acq_indCode48=10 if (sic4_acq>=3020&sic4_acq<=3021)
	replace acq_indCode48=10 if (sic4_acq>=3100&sic4_acq<=3111)
	replace acq_indCode48=10 if (sic4_acq>=3130&sic4_acq<=3131)
	replace acq_indCode48=10 if (sic4_acq>=3140&sic4_acq<=3149)
	replace acq_indCode48=10 if (sic4_acq>=3150&sic4_acq<=3151)
	replace acq_indCode48=10 if (sic4_acq>=3963&sic4_acq<=3965)
	replace acq_indCode48=11 if (sic4_acq>=8000&sic4_acq<=8099)
	replace acq_indCode48=12 if (sic4_acq>=3693&sic4_acq<=3693)
	replace acq_indCode48=12 if (sic4_acq>=3840&sic4_acq<=3849)
	replace acq_indCode48=12 if (sic4_acq>=3850&sic4_acq<=3851)
	replace acq_indCode48=13 if (sic4_acq>=2830&sic4_acq<=2830)
	replace acq_indCode48=13 if (sic4_acq>=2831&sic4_acq<=2831)
	replace acq_indCode48=13 if (sic4_acq>=2833&sic4_acq<=2833)
	replace acq_indCode48=13 if (sic4_acq>=2834&sic4_acq<=2834)
	replace acq_indCode48=13 if (sic4_acq>=2835&sic4_acq<=2835)
	replace acq_indCode48=13 if (sic4_acq>=2836&sic4_acq<=2836)
	replace acq_indCode48=14 if (sic4_acq>=2800&sic4_acq<=2809)
	replace acq_indCode48=14 if (sic4_acq>=2810&sic4_acq<=2819)
	replace acq_indCode48=14 if (sic4_acq>=2820&sic4_acq<=2829)
	replace acq_indCode48=14 if (sic4_acq>=2850&sic4_acq<=2859)
	replace acq_indCode48=14 if (sic4_acq>=2860&sic4_acq<=2869)
	replace acq_indCode48=14 if (sic4_acq>=2870&sic4_acq<=2879)
	replace acq_indCode48=14 if (sic4_acq>=2890&sic4_acq<=2899)
	replace acq_indCode48=15 if (sic4_acq>=3031&sic4_acq<=3031)
	replace acq_indCode48=15 if (sic4_acq>=3041&sic4_acq<=3041)
	replace acq_indCode48=15 if (sic4_acq>=3050&sic4_acq<=3053)
	replace acq_indCode48=15 if (sic4_acq>=3060&sic4_acq<=3069)
	replace acq_indCode48=15 if (sic4_acq>=3070&sic4_acq<=3079)
	replace acq_indCode48=15 if (sic4_acq>=3080&sic4_acq<=3089)
	replace acq_indCode48=15 if (sic4_acq>=3090&sic4_acq<=3099)
	replace acq_indCode48=16 if (sic4_acq>=2200&sic4_acq<=2269)
	replace acq_indCode48=16 if (sic4_acq>=2270&sic4_acq<=2279)
	replace acq_indCode48=16 if (sic4_acq>=2280&sic4_acq<=2284)
	replace acq_indCode48=16 if (sic4_acq>=2290&sic4_acq<=2295)
	replace acq_indCode48=16 if (sic4_acq>=2297&sic4_acq<=2297)
	replace acq_indCode48=16 if (sic4_acq>=2298&sic4_acq<=2298)
	replace acq_indCode48=16 if (sic4_acq>=2299&sic4_acq<=2299)
	replace acq_indCode48=16 if (sic4_acq>=2393&sic4_acq<=2395)
	replace acq_indCode48=16 if (sic4_acq>=2397&sic4_acq<=2399)
	replace acq_indCode48=17 if (sic4_acq>=800&sic4_acq<=899)
	replace acq_indCode48=17 if (sic4_acq>=2400&sic4_acq<=2439)
	replace acq_indCode48=17 if (sic4_acq>=2450&sic4_acq<=2459)
	replace acq_indCode48=17 if (sic4_acq>=2490&sic4_acq<=2499)
	replace acq_indCode48=17 if (sic4_acq>=2660&sic4_acq<=2661)
	replace acq_indCode48=17 if (sic4_acq>=2950&sic4_acq<=2952)
	replace acq_indCode48=17 if (sic4_acq>=3200&sic4_acq<=3200)
	replace acq_indCode48=17 if (sic4_acq>=3210&sic4_acq<=3211)
	replace acq_indCode48=17 if (sic4_acq>=3240&sic4_acq<=3241)
	replace acq_indCode48=17 if (sic4_acq>=3250&sic4_acq<=3259)
	replace acq_indCode48=17 if (sic4_acq>=3261&sic4_acq<=3261)
	replace acq_indCode48=17 if (sic4_acq>=3264&sic4_acq<=3264)
	replace acq_indCode48=17 if (sic4_acq>=3270&sic4_acq<=3275)
	replace acq_indCode48=17 if (sic4_acq>=3280&sic4_acq<=3281)
	replace acq_indCode48=17 if (sic4_acq>=3290&sic4_acq<=3293)
	replace acq_indCode48=17 if (sic4_acq>=3295&sic4_acq<=3299)
	replace acq_indCode48=17 if (sic4_acq>=3420&sic4_acq<=3429)
	replace acq_indCode48=17 if (sic4_acq>=3430&sic4_acq<=3433)
	replace acq_indCode48=17 if (sic4_acq>=3440&sic4_acq<=3441)
	replace acq_indCode48=17 if (sic4_acq>=3442&sic4_acq<=3442)
	replace acq_indCode48=17 if (sic4_acq>=3446&sic4_acq<=3446)
	replace acq_indCode48=17 if (sic4_acq>=3448&sic4_acq<=3448)
	replace acq_indCode48=17 if (sic4_acq>=3449&sic4_acq<=3449)
	replace acq_indCode48=17 if (sic4_acq>=3450&sic4_acq<=3451)
	replace acq_indCode48=17 if (sic4_acq>=3452&sic4_acq<=3452)
	replace acq_indCode48=17 if (sic4_acq>=3490&sic4_acq<=3499)
	replace acq_indCode48=17 if (sic4_acq>=3996&sic4_acq<=3996)
	replace acq_indCode48=18 if (sic4_acq>=1500&sic4_acq<=1511)
	replace acq_indCode48=18 if (sic4_acq>=1520&sic4_acq<=1529)
	replace acq_indCode48=18 if (sic4_acq>=1530&sic4_acq<=1539)
	replace acq_indCode48=18 if (sic4_acq>=1540&sic4_acq<=1549)
	replace acq_indCode48=18 if (sic4_acq>=1600&sic4_acq<=1699)
	replace acq_indCode48=18 if (sic4_acq>=1700&sic4_acq<=1799)
	replace acq_indCode48=19 if (sic4_acq>=3300&sic4_acq<=3300)
	replace acq_indCode48=19 if (sic4_acq>=3310&sic4_acq<=3317)
	replace acq_indCode48=19 if (sic4_acq>=3320&sic4_acq<=3325)
	replace acq_indCode48=19 if (sic4_acq>=3330&sic4_acq<=3339)
	replace acq_indCode48=19 if (sic4_acq>=3340&sic4_acq<=3341)
	replace acq_indCode48=19 if (sic4_acq>=3350&sic4_acq<=3357)
	replace acq_indCode48=19 if (sic4_acq>=3360&sic4_acq<=3369)
	replace acq_indCode48=19 if (sic4_acq>=3370&sic4_acq<=3379)
	replace acq_indCode48=19 if (sic4_acq>=3390&sic4_acq<=3399)
	replace acq_indCode48=20 if (sic4_acq>=3400&sic4_acq<=3400)
	replace acq_indCode48=20 if (sic4_acq>=3443&sic4_acq<=3443)
	replace acq_indCode48=20 if (sic4_acq>=3444&sic4_acq<=3444)
	replace acq_indCode48=20 if (sic4_acq>=3460&sic4_acq<=3469)
	replace acq_indCode48=20 if (sic4_acq>=3470&sic4_acq<=3479)
	replace acq_indCode48=21 if (sic4_acq>=3510&sic4_acq<=3519)
	replace acq_indCode48=21 if (sic4_acq>=3520&sic4_acq<=3529)
	replace acq_indCode48=21 if (sic4_acq>=3530&sic4_acq<=3530)
	replace acq_indCode48=21 if (sic4_acq>=3531&sic4_acq<=3531)
	replace acq_indCode48=21 if (sic4_acq>=3532&sic4_acq<=3532)
	replace acq_indCode48=21 if (sic4_acq>=3533&sic4_acq<=3533)
	replace acq_indCode48=21 if (sic4_acq>=3534&sic4_acq<=3534)
	replace acq_indCode48=21 if (sic4_acq>=3535&sic4_acq<=3535)
	replace acq_indCode48=21 if (sic4_acq>=3536&sic4_acq<=3536)
	replace acq_indCode48=21 if (sic4_acq>=3538&sic4_acq<=3538)
	replace acq_indCode48=21 if (sic4_acq>=3540&sic4_acq<=3549)
	replace acq_indCode48=21 if (sic4_acq>=3550&sic4_acq<=3559)
	replace acq_indCode48=21 if (sic4_acq>=3560&sic4_acq<=3569)
	replace acq_indCode48=21 if (sic4_acq>=3580&sic4_acq<=3580)
	replace acq_indCode48=21 if (sic4_acq>=3581&sic4_acq<=3581)
	replace acq_indCode48=21 if (sic4_acq>=3582&sic4_acq<=3582)
	replace acq_indCode48=21 if (sic4_acq>=3585&sic4_acq<=3585)
	replace acq_indCode48=21 if (sic4_acq>=3586&sic4_acq<=3586)
	replace acq_indCode48=21 if (sic4_acq>=3589&sic4_acq<=3589)
	replace acq_indCode48=21 if (sic4_acq>=3590&sic4_acq<=3599)
	replace acq_indCode48=22 if (sic4_acq>=3600&sic4_acq<=3600)
	replace acq_indCode48=22 if (sic4_acq>=3610&sic4_acq<=3613)
	replace acq_indCode48=22 if (sic4_acq>=3620&sic4_acq<=3621)
	replace acq_indCode48=22 if (sic4_acq>=3623&sic4_acq<=3629)
	replace acq_indCode48=22 if (sic4_acq>=3640&sic4_acq<=3644)
	replace acq_indCode48=22 if (sic4_acq>=3645&sic4_acq<=3645)
	replace acq_indCode48=22 if (sic4_acq>=3646&sic4_acq<=3646)
	replace acq_indCode48=22 if (sic4_acq>=3648&sic4_acq<=3649)
	replace acq_indCode48=22 if (sic4_acq>=3660&sic4_acq<=3660)
	replace acq_indCode48=22 if (sic4_acq>=3690&sic4_acq<=3690)
	replace acq_indCode48=22 if (sic4_acq>=3691&sic4_acq<=3692)
	replace acq_indCode48=22 if (sic4_acq>=3699&sic4_acq<=3699)
	replace acq_indCode48=23 if (sic4_acq>=2296&sic4_acq<=2296)
	replace acq_indCode48=23 if (sic4_acq>=2396&sic4_acq<=2396)
	replace acq_indCode48=23 if (sic4_acq>=3010&sic4_acq<=3011)
	replace acq_indCode48=23 if (sic4_acq>=3537&sic4_acq<=3537)
	replace acq_indCode48=23 if (sic4_acq>=3647&sic4_acq<=3647)
	replace acq_indCode48=23 if (sic4_acq>=3694&sic4_acq<=3694)
	replace acq_indCode48=23 if (sic4_acq>=3700&sic4_acq<=3700)
	replace acq_indCode48=23 if (sic4_acq>=3710&sic4_acq<=3710)
	replace acq_indCode48=23 if (sic4_acq>=3711&sic4_acq<=3711)
	replace acq_indCode48=23 if (sic4_acq>=3713&sic4_acq<=3713)
	replace acq_indCode48=23 if (sic4_acq>=3714&sic4_acq<=3714)
	replace acq_indCode48=23 if (sic4_acq>=3715&sic4_acq<=3715)
	replace acq_indCode48=23 if (sic4_acq>=3716&sic4_acq<=3716)
	replace acq_indCode48=23 if (sic4_acq>=3792&sic4_acq<=3792)
	replace acq_indCode48=23 if (sic4_acq>=3790&sic4_acq<=3791)
	replace acq_indCode48=23 if (sic4_acq>=3799&sic4_acq<=3799)
	replace acq_indCode48=24 if (sic4_acq>=3720&sic4_acq<=3720)
	replace acq_indCode48=24 if (sic4_acq>=3721&sic4_acq<=3721)
	replace acq_indCode48=24 if (sic4_acq>=3723&sic4_acq<=3724)
	replace acq_indCode48=24 if (sic4_acq>=3725&sic4_acq<=3725)
	replace acq_indCode48=24 if (sic4_acq>=3728&sic4_acq<=3729)
	replace acq_indCode48=25 if (sic4_acq>=3730&sic4_acq<=3731)
	replace acq_indCode48=25 if (sic4_acq>=3740&sic4_acq<=3743)
	replace acq_indCode48=26 if (sic4_acq>=3760&sic4_acq<=3769)
	replace acq_indCode48=26 if (sic4_acq>=3795&sic4_acq<=3795)
	replace acq_indCode48=26 if (sic4_acq>=3480&sic4_acq<=3489)
	replace acq_indCode48=27 if (sic4_acq>=1040&sic4_acq<=1049)
	replace acq_indCode48=28 if (sic4_acq>=1000&sic4_acq<=1009)
	replace acq_indCode48=28 if (sic4_acq>=1010&sic4_acq<=1019)
	replace acq_indCode48=28 if (sic4_acq>=1020&sic4_acq<=1029)
	replace acq_indCode48=28 if (sic4_acq>=1030&sic4_acq<=1039)
	replace acq_indCode48=28 if (sic4_acq>=1050&sic4_acq<=1059)
	replace acq_indCode48=28 if (sic4_acq>=1060&sic4_acq<=1069)
	replace acq_indCode48=28 if (sic4_acq>=1070&sic4_acq<=1079)
	replace acq_indCode48=28 if (sic4_acq>=1080&sic4_acq<=1089)
	replace acq_indCode48=28 if (sic4_acq>=1090&sic4_acq<=1099)
	replace acq_indCode48=28 if (sic4_acq>=1100&sic4_acq<=1119)
	replace acq_indCode48=28 if (sic4_acq>=1400&sic4_acq<=1499)
	replace acq_indCode48=29 if (sic4_acq>=1200&sic4_acq<=1299)
	replace acq_indCode48=30 if (sic4_acq>=1300&sic4_acq<=1300)
	replace acq_indCode48=30 if (sic4_acq>=1310&sic4_acq<=1319)
	replace acq_indCode48=30 if (sic4_acq>=1320&sic4_acq<=1329)
	replace acq_indCode48=30 if (sic4_acq>=1330&sic4_acq<=1339)
	replace acq_indCode48=30 if (sic4_acq>=1370&sic4_acq<=1379)
	replace acq_indCode48=30 if (sic4_acq>=1380&sic4_acq<=1380)
	replace acq_indCode48=30 if (sic4_acq>=1381&sic4_acq<=1381)
	replace acq_indCode48=30 if (sic4_acq>=1382&sic4_acq<=1382)
	replace acq_indCode48=30 if (sic4_acq>=1389&sic4_acq<=1389)
	replace acq_indCode48=30 if (sic4_acq>=2900&sic4_acq<=2912)
	replace acq_indCode48=30 if (sic4_acq>=2990&sic4_acq<=2999)
	replace acq_indCode48=31 if (sic4_acq>=4900&sic4_acq<=4900)
	replace acq_indCode48=31 if (sic4_acq>=4910&sic4_acq<=4911)
	replace acq_indCode48=31 if (sic4_acq>=4920&sic4_acq<=4922)
	replace acq_indCode48=31 if (sic4_acq>=4923&sic4_acq<=4923)
	replace acq_indCode48=31 if (sic4_acq>=4924&sic4_acq<=4925)
	replace acq_indCode48=31 if (sic4_acq>=4930&sic4_acq<=4931)
	replace acq_indCode48=31 if (sic4_acq>=4932&sic4_acq<=4932)
	replace acq_indCode48=31 if (sic4_acq>=4939&sic4_acq<=4939)
	replace acq_indCode48=31 if (sic4_acq>=4940&sic4_acq<=4942)
	replace acq_indCode48=32 if (sic4_acq>=4800&sic4_acq<=4800)
	replace acq_indCode48=32 if (sic4_acq>=4810&sic4_acq<=4813)
	replace acq_indCode48=32 if (sic4_acq>=4820&sic4_acq<=4822)
	replace acq_indCode48=32 if (sic4_acq>=4830&sic4_acq<=4839)
	replace acq_indCode48=32 if (sic4_acq>=4840&sic4_acq<=4841)
	replace acq_indCode48=32 if (sic4_acq>=4880&sic4_acq<=4889)
	replace acq_indCode48=32 if (sic4_acq>=4890&sic4_acq<=4890)
	replace acq_indCode48=32 if (sic4_acq>=4891&sic4_acq<=4891)
	replace acq_indCode48=32 if (sic4_acq>=4892&sic4_acq<=4892)
	replace acq_indCode48=32 if (sic4_acq>=4899&sic4_acq<=4899)
	replace acq_indCode48=33 if (sic4_acq>=7020&sic4_acq<=7021)
	replace acq_indCode48=33 if (sic4_acq>=7030&sic4_acq<=7033)
	replace acq_indCode48=33 if (sic4_acq>=7200&sic4_acq<=7200)
	replace acq_indCode48=33 if (sic4_acq>=7210&sic4_acq<=7212)
	replace acq_indCode48=33 if (sic4_acq>=7214&sic4_acq<=7214)
	replace acq_indCode48=33 if (sic4_acq>=7215&sic4_acq<=7216)
	replace acq_indCode48=33 if (sic4_acq>=7217&sic4_acq<=7217)
	replace acq_indCode48=33 if (sic4_acq>=7219&sic4_acq<=7219)
	replace acq_indCode48=33 if (sic4_acq>=7220&sic4_acq<=7221)
	replace acq_indCode48=33 if (sic4_acq>=7230&sic4_acq<=7231)
	replace acq_indCode48=33 if (sic4_acq>=7240&sic4_acq<=7241)
	replace acq_indCode48=33 if (sic4_acq>=7250&sic4_acq<=7251)
	replace acq_indCode48=33 if (sic4_acq>=7260&sic4_acq<=7269)
	replace acq_indCode48=33 if (sic4_acq>=7270&sic4_acq<=7290)
	replace acq_indCode48=33 if (sic4_acq>=7291&sic4_acq<=7291)
	replace acq_indCode48=33 if (sic4_acq>=7292&sic4_acq<=7299)
	replace acq_indCode48=33 if (sic4_acq>=7395&sic4_acq<=7395)
	replace acq_indCode48=33 if (sic4_acq>=7500&sic4_acq<=7500)
	replace acq_indCode48=33 if (sic4_acq>=7520&sic4_acq<=7529)
	replace acq_indCode48=33 if (sic4_acq>=7530&sic4_acq<=7539)
	replace acq_indCode48=33 if (sic4_acq>=7540&sic4_acq<=7549)
	replace acq_indCode48=33 if (sic4_acq>=7600&sic4_acq<=7600)
	replace acq_indCode48=33 if (sic4_acq>=7620&sic4_acq<=7620)
	replace acq_indCode48=33 if (sic4_acq>=7622&sic4_acq<=7622)
	replace acq_indCode48=33 if (sic4_acq>=7623&sic4_acq<=7623)
	replace acq_indCode48=33 if (sic4_acq>=7629&sic4_acq<=7629)
	replace acq_indCode48=33 if (sic4_acq>=7630&sic4_acq<=7631)
	replace acq_indCode48=33 if (sic4_acq>=7640&sic4_acq<=7641)
	replace acq_indCode48=33 if (sic4_acq>=7690&sic4_acq<=7699)
	replace acq_indCode48=33 if (sic4_acq>=8100&sic4_acq<=8199)
	replace acq_indCode48=33 if (sic4_acq>=8200&sic4_acq<=8299)
	replace acq_indCode48=33 if (sic4_acq>=8300&sic4_acq<=8399)
	replace acq_indCode48=33 if (sic4_acq>=8400&sic4_acq<=8499)
	replace acq_indCode48=33 if (sic4_acq>=8600&sic4_acq<=8699)
	replace acq_indCode48=33 if (sic4_acq>=8800&sic4_acq<=8899)
	replace acq_indCode48=33 if (sic4_acq>=7510&sic4_acq<=7515)
	replace acq_indCode48=34 if (sic4_acq>=2750&sic4_acq<=2759)
	replace acq_indCode48=34 if (sic4_acq>=3993&sic4_acq<=3993)
	replace acq_indCode48=34 if (sic4_acq>=7218&sic4_acq<=7218)
	replace acq_indCode48=34 if (sic4_acq>=7300&sic4_acq<=7300)
	replace acq_indCode48=34 if (sic4_acq>=7310&sic4_acq<=7319)
	replace acq_indCode48=34 if (sic4_acq>=7320&sic4_acq<=7329)
	replace acq_indCode48=34 if (sic4_acq>=7330&sic4_acq<=7339)
	replace acq_indCode48=34 if (sic4_acq>=7340&sic4_acq<=7342)
	replace acq_indCode48=34 if (sic4_acq>=7349&sic4_acq<=7349)
	replace acq_indCode48=34 if (sic4_acq>=7350&sic4_acq<=7351)
	replace acq_indCode48=34 if (sic4_acq>=7352&sic4_acq<=7352)
	replace acq_indCode48=34 if (sic4_acq>=7353&sic4_acq<=7353)
	replace acq_indCode48=34 if (sic4_acq>=7359&sic4_acq<=7359)
	replace acq_indCode48=34 if (sic4_acq>=7360&sic4_acq<=7369)
	replace acq_indCode48=34 if (sic4_acq>=7370&sic4_acq<=7372)
	replace acq_indCode48=34 if (sic4_acq>=7374&sic4_acq<=7374)
	replace acq_indCode48=34 if (sic4_acq>=7375&sic4_acq<=7375)
	replace acq_indCode48=34 if (sic4_acq>=7376&sic4_acq<=7376)
	replace acq_indCode48=34 if (sic4_acq>=7377&sic4_acq<=7377)
	replace acq_indCode48=34 if (sic4_acq>=7378&sic4_acq<=7378)
	replace acq_indCode48=34 if (sic4_acq>=7379&sic4_acq<=7379)
	replace acq_indCode48=34 if (sic4_acq>=7380&sic4_acq<=7380)
	replace acq_indCode48=34 if (sic4_acq>=7381&sic4_acq<=7382)
	replace acq_indCode48=34 if (sic4_acq>=7383&sic4_acq<=7383)
	replace acq_indCode48=34 if (sic4_acq>=7384&sic4_acq<=7384)
	replace acq_indCode48=34 if (sic4_acq>=7385&sic4_acq<=7385)
	replace acq_indCode48=34 if (sic4_acq>=7389&sic4_acq<=7390)
	replace acq_indCode48=34 if (sic4_acq>=7391&sic4_acq<=7391)
	replace acq_indCode48=34 if (sic4_acq>=7392&sic4_acq<=7392)
	replace acq_indCode48=34 if (sic4_acq>=7393&sic4_acq<=7393)
	replace acq_indCode48=34 if (sic4_acq>=7394&sic4_acq<=7394)
	replace acq_indCode48=34 if (sic4_acq>=7396&sic4_acq<=7396)
	replace acq_indCode48=34 if (sic4_acq>=7397&sic4_acq<=7397)
	replace acq_indCode48=34 if (sic4_acq>=7399&sic4_acq<=7399)
	replace acq_indCode48=34 if (sic4_acq>=7519&sic4_acq<=7519)
	replace acq_indCode48=34 if (sic4_acq>=8700&sic4_acq<=8700)
	replace acq_indCode48=34 if (sic4_acq>=8710&sic4_acq<=8713)
	replace acq_indCode48=34 if (sic4_acq>=8720&sic4_acq<=8721)
	replace acq_indCode48=34 if (sic4_acq>=8730&sic4_acq<=8734)
	replace acq_indCode48=34 if (sic4_acq>=8740&sic4_acq<=8748)
	replace acq_indCode48=34 if (sic4_acq>=8900&sic4_acq<=8910)
	replace acq_indCode48=34 if (sic4_acq>=8911&sic4_acq<=8911)
	replace acq_indCode48=34 if (sic4_acq>=8920&sic4_acq<=8999)
	replace acq_indCode48=34 if (sic4_acq>=4220&sic4_acq<=4229)
	replace acq_indCode48=35 if (sic4_acq>=3570&sic4_acq<=3579)
	replace acq_indCode48=35 if (sic4_acq>=3680&sic4_acq<=3680)
	replace acq_indCode48=35 if (sic4_acq>=3681&sic4_acq<=3681)
	replace acq_indCode48=35 if (sic4_acq>=3682&sic4_acq<=3682)
	replace acq_indCode48=35 if (sic4_acq>=3683&sic4_acq<=3683)
	replace acq_indCode48=35 if (sic4_acq>=3684&sic4_acq<=3684)
	replace acq_indCode48=35 if (sic4_acq>=3685&sic4_acq<=3685)
	replace acq_indCode48=35 if (sic4_acq>=3686&sic4_acq<=3686)
	replace acq_indCode48=35 if (sic4_acq>=3687&sic4_acq<=3687)
	replace acq_indCode48=35 if (sic4_acq>=3688&sic4_acq<=3688)
	replace acq_indCode48=35 if (sic4_acq>=3689&sic4_acq<=3689)
	replace acq_indCode48=35 if (sic4_acq>=3695&sic4_acq<=3695)
	replace acq_indCode48=35 if (sic4_acq>=7373&sic4_acq<=7373)
	replace acq_indCode48=36 if (sic4_acq>=3622&sic4_acq<=3622)
	replace acq_indCode48=36 if (sic4_acq>=3661&sic4_acq<=3661)
	replace acq_indCode48=36 if (sic4_acq>=3662&sic4_acq<=3662)
	replace acq_indCode48=36 if (sic4_acq>=3663&sic4_acq<=3663)
	replace acq_indCode48=36 if (sic4_acq>=3664&sic4_acq<=3664)
	replace acq_indCode48=36 if (sic4_acq>=3665&sic4_acq<=3665)
	replace acq_indCode48=36 if (sic4_acq>=3666&sic4_acq<=3666)
	replace acq_indCode48=36 if (sic4_acq>=3669&sic4_acq<=3669)
	replace acq_indCode48=36 if (sic4_acq>=3670&sic4_acq<=3679)
	replace acq_indCode48=36 if (sic4_acq>=3810&sic4_acq<=3810)
	replace acq_indCode48=36 if (sic4_acq>=3812&sic4_acq<=3812)
	replace acq_indCode48=37 if (sic4_acq>=3811&sic4_acq<=3811)
	replace acq_indCode48=37 if (sic4_acq>=3820&sic4_acq<=3820)
	replace acq_indCode48=37 if (sic4_acq>=3821&sic4_acq<=3821)
	replace acq_indCode48=37 if (sic4_acq>=3822&sic4_acq<=3822)
	replace acq_indCode48=37 if (sic4_acq>=3823&sic4_acq<=3823)
	replace acq_indCode48=37 if (sic4_acq>=3824&sic4_acq<=3824)
	replace acq_indCode48=37 if (sic4_acq>=3825&sic4_acq<=3825)
	replace acq_indCode48=37 if (sic4_acq>=3826&sic4_acq<=3826)
	replace acq_indCode48=37 if (sic4_acq>=3827&sic4_acq<=3827)
	replace acq_indCode48=37 if (sic4_acq>=3829&sic4_acq<=3829)
	replace acq_indCode48=37 if (sic4_acq>=3830&sic4_acq<=3839)
	replace acq_indCode48=38 if (sic4_acq>=2520&sic4_acq<=2549)
	replace acq_indCode48=38 if (sic4_acq>=2600&sic4_acq<=2639)
	replace acq_indCode48=38 if (sic4_acq>=2670&sic4_acq<=2699)
	replace acq_indCode48=38 if (sic4_acq>=2760&sic4_acq<=2761)
	replace acq_indCode48=38 if (sic4_acq>=3950&sic4_acq<=3955)
	replace acq_indCode48=39 if (sic4_acq>=2440&sic4_acq<=2449)
	replace acq_indCode48=39 if (sic4_acq>=2640&sic4_acq<=2659)
	replace acq_indCode48=39 if (sic4_acq>=3220&sic4_acq<=3221)
	replace acq_indCode48=39 if (sic4_acq>=3410&sic4_acq<=3412)
	replace acq_indCode48=40 if (sic4_acq>=4000&sic4_acq<=4013)
	replace acq_indCode48=40 if (sic4_acq>=4040&sic4_acq<=4049)
	replace acq_indCode48=40 if (sic4_acq>=4100&sic4_acq<=4100)
	replace acq_indCode48=40 if (sic4_acq>=4110&sic4_acq<=4119)
	replace acq_indCode48=40 if (sic4_acq>=4120&sic4_acq<=4121)
	replace acq_indCode48=40 if (sic4_acq>=4130&sic4_acq<=4131)
	replace acq_indCode48=40 if (sic4_acq>=4140&sic4_acq<=4142)
	replace acq_indCode48=40 if (sic4_acq>=4150&sic4_acq<=4151)
	replace acq_indCode48=40 if (sic4_acq>=4170&sic4_acq<=4173)
	replace acq_indCode48=40 if (sic4_acq>=4190&sic4_acq<=4199)
	replace acq_indCode48=40 if (sic4_acq>=4200&sic4_acq<=4200)
	replace acq_indCode48=40 if (sic4_acq>=4210&sic4_acq<=4219)
	replace acq_indCode48=40 if (sic4_acq>=4230&sic4_acq<=4231)
	replace acq_indCode48=40 if (sic4_acq>=4240&sic4_acq<=4249)
	replace acq_indCode48=40 if (sic4_acq>=4400&sic4_acq<=4499)
	replace acq_indCode48=40 if (sic4_acq>=4500&sic4_acq<=4599)
	replace acq_indCode48=40 if (sic4_acq>=4600&sic4_acq<=4699)
	replace acq_indCode48=40 if (sic4_acq>=4700&sic4_acq<=4700)
	replace acq_indCode48=40 if (sic4_acq>=4710&sic4_acq<=4712)
	replace acq_indCode48=40 if (sic4_acq>=4720&sic4_acq<=4729)
	replace acq_indCode48=40 if (sic4_acq>=4730&sic4_acq<=4739)
	replace acq_indCode48=40 if (sic4_acq>=4740&sic4_acq<=4749)
	replace acq_indCode48=40 if (sic4_acq>=4780&sic4_acq<=4780)
	replace acq_indCode48=40 if (sic4_acq>=4782&sic4_acq<=4782)
	replace acq_indCode48=40 if (sic4_acq>=4783&sic4_acq<=4783)
	replace acq_indCode48=40 if (sic4_acq>=4784&sic4_acq<=4784)
	replace acq_indCode48=40 if (sic4_acq>=4785&sic4_acq<=4785)
	replace acq_indCode48=40 if (sic4_acq>=4789&sic4_acq<=4789)
	replace acq_indCode48=41 if (sic4_acq>=5000&sic4_acq<=5000)
	replace acq_indCode48=41 if (sic4_acq>=5010&sic4_acq<=5015)
	replace acq_indCode48=41 if (sic4_acq>=5020&sic4_acq<=5023)
	replace acq_indCode48=41 if (sic4_acq>=5030&sic4_acq<=5039)
	replace acq_indCode48=41 if (sic4_acq>=5040&sic4_acq<=5042)
	replace acq_indCode48=41 if (sic4_acq>=5043&sic4_acq<=5043)
	replace acq_indCode48=41 if (sic4_acq>=5044&sic4_acq<=5044)
	replace acq_indCode48=41 if (sic4_acq>=5045&sic4_acq<=5045)
	replace acq_indCode48=41 if (sic4_acq>=5046&sic4_acq<=5046)
	replace acq_indCode48=41 if (sic4_acq>=5047&sic4_acq<=5047)
	replace acq_indCode48=41 if (sic4_acq>=5048&sic4_acq<=5048)
	replace acq_indCode48=41 if (sic4_acq>=5049&sic4_acq<=5049)
	replace acq_indCode48=41 if (sic4_acq>=5050&sic4_acq<=5059)
	replace acq_indCode48=41 if (sic4_acq>=5060&sic4_acq<=5060)
	replace acq_indCode48=41 if (sic4_acq>=5063&sic4_acq<=5063)
	replace acq_indCode48=41 if (sic4_acq>=5064&sic4_acq<=5064)
	replace acq_indCode48=41 if (sic4_acq>=5065&sic4_acq<=5065)
	replace acq_indCode48=41 if (sic4_acq>=5070&sic4_acq<=5078)
	replace acq_indCode48=41 if (sic4_acq>=5080&sic4_acq<=5080)
	replace acq_indCode48=41 if (sic4_acq>=5081&sic4_acq<=5081)
	replace acq_indCode48=41 if (sic4_acq>=5082&sic4_acq<=5082)
	replace acq_indCode48=41 if (sic4_acq>=5083&sic4_acq<=5083)
	replace acq_indCode48=41 if (sic4_acq>=5084&sic4_acq<=5084)
	replace acq_indCode48=41 if (sic4_acq>=5085&sic4_acq<=5085)
	replace acq_indCode48=41 if (sic4_acq>=5086&sic4_acq<=5087)
	replace acq_indCode48=41 if (sic4_acq>=5088&sic4_acq<=5088)
	replace acq_indCode48=41 if (sic4_acq>=5090&sic4_acq<=5090)
	replace acq_indCode48=41 if (sic4_acq>=5091&sic4_acq<=5092)
	replace acq_indCode48=41 if (sic4_acq>=5093&sic4_acq<=5093)
	replace acq_indCode48=41 if (sic4_acq>=5094&sic4_acq<=5094)
	replace acq_indCode48=41 if (sic4_acq>=5099&sic4_acq<=5099)
	replace acq_indCode48=41 if (sic4_acq>=5100&sic4_acq<=5100)
	replace acq_indCode48=41 if (sic4_acq>=5110&sic4_acq<=5113)
	replace acq_indCode48=41 if (sic4_acq>=5120&sic4_acq<=5122)
	replace acq_indCode48=41 if (sic4_acq>=5130&sic4_acq<=5139)
	replace acq_indCode48=41 if (sic4_acq>=5140&sic4_acq<=5149)
	replace acq_indCode48=41 if (sic4_acq>=5150&sic4_acq<=5159)
	replace acq_indCode48=41 if (sic4_acq>=5160&sic4_acq<=5169)
	replace acq_indCode48=41 if (sic4_acq>=5170&sic4_acq<=5172)
	replace acq_indCode48=41 if (sic4_acq>=5180&sic4_acq<=5182)
	replace acq_indCode48=41 if (sic4_acq>=5190&sic4_acq<=5199)
	replace acq_indCode48=42 if (sic4_acq>=5200&sic4_acq<=5200)
	replace acq_indCode48=42 if (sic4_acq>=5210&sic4_acq<=5219)
	replace acq_indCode48=42 if (sic4_acq>=5220&sic4_acq<=5229)
	replace acq_indCode48=42 if (sic4_acq>=5230&sic4_acq<=5231)
	replace acq_indCode48=42 if (sic4_acq>=5250&sic4_acq<=5251)
	replace acq_indCode48=42 if (sic4_acq>=5260&sic4_acq<=5261)
	replace acq_indCode48=42 if (sic4_acq>=5270&sic4_acq<=5271)
	replace acq_indCode48=42 if (sic4_acq>=5300&sic4_acq<=5300)
	replace acq_indCode48=42 if (sic4_acq>=5310&sic4_acq<=5311)
	replace acq_indCode48=42 if (sic4_acq>=5320&sic4_acq<=5320)
	replace acq_indCode48=42 if (sic4_acq>=5330&sic4_acq<=5331)
	replace acq_indCode48=42 if (sic4_acq>=5334&sic4_acq<=5334)
	replace acq_indCode48=42 if (sic4_acq>=5340&sic4_acq<=5349)
	replace acq_indCode48=42 if (sic4_acq>=5390&sic4_acq<=5399)
	replace acq_indCode48=42 if (sic4_acq>=5400&sic4_acq<=5400)
	replace acq_indCode48=42 if (sic4_acq>=5410&sic4_acq<=5411)
	replace acq_indCode48=42 if (sic4_acq>=5412&sic4_acq<=5412)
	replace acq_indCode48=42 if (sic4_acq>=5420&sic4_acq<=5429)
	replace acq_indCode48=42 if (sic4_acq>=5430&sic4_acq<=5439)
	replace acq_indCode48=42 if (sic4_acq>=5440&sic4_acq<=5449)
	replace acq_indCode48=42 if (sic4_acq>=5450&sic4_acq<=5459)
	replace acq_indCode48=42 if (sic4_acq>=5460&sic4_acq<=5469)
	replace acq_indCode48=42 if (sic4_acq>=5490&sic4_acq<=5499)
	replace acq_indCode48=42 if (sic4_acq>=5500&sic4_acq<=5500)
	replace acq_indCode48=42 if (sic4_acq>=5510&sic4_acq<=5529)
	replace acq_indCode48=42 if (sic4_acq>=5530&sic4_acq<=5539)
	replace acq_indCode48=42 if (sic4_acq>=5540&sic4_acq<=5549)
	replace acq_indCode48=42 if (sic4_acq>=5550&sic4_acq<=5559)
	replace acq_indCode48=42 if (sic4_acq>=5560&sic4_acq<=5569)
	replace acq_indCode48=42 if (sic4_acq>=5570&sic4_acq<=5579)
	replace acq_indCode48=42 if (sic4_acq>=5590&sic4_acq<=5599)
	replace acq_indCode48=42 if (sic4_acq>=5600&sic4_acq<=5699)
	replace acq_indCode48=42 if (sic4_acq>=5700&sic4_acq<=5700)
	replace acq_indCode48=42 if (sic4_acq>=5710&sic4_acq<=5719)
	replace acq_indCode48=42 if (sic4_acq>=5720&sic4_acq<=5722)
	replace acq_indCode48=42 if (sic4_acq>=5730&sic4_acq<=5733)
	replace acq_indCode48=42 if (sic4_acq>=5734&sic4_acq<=5734)
	replace acq_indCode48=42 if (sic4_acq>=5735&sic4_acq<=5735)
	replace acq_indCode48=42 if (sic4_acq>=5736&sic4_acq<=5736)
	replace acq_indCode48=42 if (sic4_acq>=5750&sic4_acq<=5799)
	replace acq_indCode48=42 if (sic4_acq>=5900&sic4_acq<=5900)
	replace acq_indCode48=42 if (sic4_acq>=5910&sic4_acq<=5912)
	replace acq_indCode48=42 if (sic4_acq>=5920&sic4_acq<=5929)
	replace acq_indCode48=42 if (sic4_acq>=5930&sic4_acq<=5932)
	replace acq_indCode48=42 if (sic4_acq>=5940&sic4_acq<=5940)
	replace acq_indCode48=42 if (sic4_acq>=5941&sic4_acq<=5941)
	replace acq_indCode48=42 if (sic4_acq>=5942&sic4_acq<=5942)
	replace acq_indCode48=42 if (sic4_acq>=5943&sic4_acq<=5943)
	replace acq_indCode48=42 if (sic4_acq>=5944&sic4_acq<=5944)
	replace acq_indCode48=42 if (sic4_acq>=5945&sic4_acq<=5945)
	replace acq_indCode48=42 if (sic4_acq>=5946&sic4_acq<=5946)
	replace acq_indCode48=42 if (sic4_acq>=5947&sic4_acq<=5947)
	replace acq_indCode48=42 if (sic4_acq>=5948&sic4_acq<=5948)
	replace acq_indCode48=42 if (sic4_acq>=5949&sic4_acq<=5949)
	replace acq_indCode48=42 if (sic4_acq>=5950&sic4_acq<=5959)
	replace acq_indCode48=42 if (sic4_acq>=5960&sic4_acq<=5969)
	replace acq_indCode48=42 if (sic4_acq>=5970&sic4_acq<=5979)
	replace acq_indCode48=42 if (sic4_acq>=5980&sic4_acq<=5989)
	replace acq_indCode48=42 if (sic4_acq>=5990&sic4_acq<=5990)
	replace acq_indCode48=42 if (sic4_acq>=5992&sic4_acq<=5992)
	replace acq_indCode48=42 if (sic4_acq>=5993&sic4_acq<=5993)
	replace acq_indCode48=42 if (sic4_acq>=5994&sic4_acq<=5994)
	replace acq_indCode48=42 if (sic4_acq>=5995&sic4_acq<=5995)
	replace acq_indCode48=42 if (sic4_acq>=5999&sic4_acq<=5999)
	replace acq_indCode48=43 if (sic4_acq>=5800&sic4_acq<=5819)
	replace acq_indCode48=43 if (sic4_acq>=5820&sic4_acq<=5829)
	replace acq_indCode48=43 if (sic4_acq>=5890&sic4_acq<=5899)
	replace acq_indCode48=43 if (sic4_acq>=7000&sic4_acq<=7000)
	replace acq_indCode48=43 if (sic4_acq>=7010&sic4_acq<=7019)
	replace acq_indCode48=43 if (sic4_acq>=7040&sic4_acq<=7049)
	replace acq_indCode48=43 if (sic4_acq>=7213&sic4_acq<=7213)
	replace acq_indCode48=44 if (sic4_acq>=6000&sic4_acq<=6000)
	replace acq_indCode48=44 if (sic4_acq>=6010&sic4_acq<=6019)
	replace acq_indCode48=44 if (sic4_acq>=6020&sic4_acq<=6020)
	replace acq_indCode48=44 if (sic4_acq>=6021&sic4_acq<=6021)
	replace acq_indCode48=44 if (sic4_acq>=6022&sic4_acq<=6022)
	replace acq_indCode48=44 if (sic4_acq>=6023&sic4_acq<=6024)
	replace acq_indCode48=44 if (sic4_acq>=6025&sic4_acq<=6025)
	replace acq_indCode48=44 if (sic4_acq>=6026&sic4_acq<=6026)
	replace acq_indCode48=44 if (sic4_acq>=6027&sic4_acq<=6027)
	replace acq_indCode48=44 if (sic4_acq>=6028&sic4_acq<=6029)
	replace acq_indCode48=44 if (sic4_acq>=6030&sic4_acq<=6036)
	replace acq_indCode48=44 if (sic4_acq>=6040&sic4_acq<=6059)
	replace acq_indCode48=44 if (sic4_acq>=6060&sic4_acq<=6062)
	replace acq_indCode48=44 if (sic4_acq>=6080&sic4_acq<=6082)
	replace acq_indCode48=44 if (sic4_acq>=6090&sic4_acq<=6099)
	replace acq_indCode48=44 if (sic4_acq>=6100&sic4_acq<=6100)
	replace acq_indCode48=44 if (sic4_acq>=6110&sic4_acq<=6111)
	replace acq_indCode48=44 if (sic4_acq>=6112&sic4_acq<=6113)
	replace acq_indCode48=44 if (sic4_acq>=6120&sic4_acq<=6129)
	replace acq_indCode48=44 if (sic4_acq>=6130&sic4_acq<=6139)
	replace acq_indCode48=44 if (sic4_acq>=6140&sic4_acq<=6149)
	replace acq_indCode48=44 if (sic4_acq>=6150&sic4_acq<=6159)
	replace acq_indCode48=44 if (sic4_acq>=6160&sic4_acq<=6169)
	replace acq_indCode48=44 if (sic4_acq>=6170&sic4_acq<=6179)
	replace acq_indCode48=44 if (sic4_acq>=6190&sic4_acq<=6199)
	replace acq_indCode48=45 if (sic4_acq>=6300&sic4_acq<=6300)
	replace acq_indCode48=45 if (sic4_acq>=6310&sic4_acq<=6319)
	replace acq_indCode48=45 if (sic4_acq>=6320&sic4_acq<=6329)
	replace acq_indCode48=45 if (sic4_acq>=6330&sic4_acq<=6331)
	replace acq_indCode48=45 if (sic4_acq>=6350&sic4_acq<=6351)
	replace acq_indCode48=45 if (sic4_acq>=6360&sic4_acq<=6361)
	replace acq_indCode48=45 if (sic4_acq>=6370&sic4_acq<=6379)
	replace acq_indCode48=45 if (sic4_acq>=6390&sic4_acq<=6399)
	replace acq_indCode48=45 if (sic4_acq>=6400&sic4_acq<=6411)
	replace acq_indCode48=46 if (sic4_acq>=6500&sic4_acq<=6500)
	replace acq_indCode48=46 if (sic4_acq>=6510&sic4_acq<=6510)
	replace acq_indCode48=46 if (sic4_acq>=6512&sic4_acq<=6512)
	replace acq_indCode48=46 if (sic4_acq>=6513&sic4_acq<=6513)
	replace acq_indCode48=46 if (sic4_acq>=6514&sic4_acq<=6514)
	replace acq_indCode48=46 if (sic4_acq>=6515&sic4_acq<=6515)
	replace acq_indCode48=46 if (sic4_acq>=6517&sic4_acq<=6519)
	replace acq_indCode48=46 if (sic4_acq>=6520&sic4_acq<=6529)
	replace acq_indCode48=46 if (sic4_acq>=6530&sic4_acq<=6531)
	replace acq_indCode48=46 if (sic4_acq>=6532&sic4_acq<=6532)
	replace acq_indCode48=46 if (sic4_acq>=6540&sic4_acq<=6541)
	replace acq_indCode48=46 if (sic4_acq>=6550&sic4_acq<=6553)
	replace acq_indCode48=46 if (sic4_acq>=6590&sic4_acq<=6599)
	replace acq_indCode48=46 if (sic4_acq>=6610&sic4_acq<=6611)
	replace acq_indCode48=47 if (sic4_acq>=6200&sic4_acq<=6299)
	replace acq_indCode48=47 if (sic4_acq>=6700&sic4_acq<=6700)
	replace acq_indCode48=47 if (sic4_acq>=6710&sic4_acq<=6719)
	replace acq_indCode48=47 if (sic4_acq>=6720&sic4_acq<=6722)
	replace acq_indCode48=47 if (sic4_acq>=6723&sic4_acq<=6723)
	replace acq_indCode48=47 if (sic4_acq>=6724&sic4_acq<=6724)
	replace acq_indCode48=47 if (sic4_acq>=6725&sic4_acq<=6725)
	replace acq_indCode48=47 if (sic4_acq>=6726&sic4_acq<=6726)
	replace acq_indCode48=47 if (sic4_acq>=6730&sic4_acq<=6733)
	replace acq_indCode48=47 if (sic4_acq>=6740&sic4_acq<=6779)
	replace acq_indCode48=47 if (sic4_acq>=6790&sic4_acq<=6791)
	replace acq_indCode48=47 if (sic4_acq>=6792&sic4_acq<=6792)
	replace acq_indCode48=47 if (sic4_acq>=6793&sic4_acq<=6793)
	replace acq_indCode48=47 if (sic4_acq>=6794&sic4_acq<=6794)
	replace acq_indCode48=47 if (sic4_acq>=6795&sic4_acq<=6795)
	replace acq_indCode48=47 if (sic4_acq>=6798&sic4_acq<=6798)
	replace acq_indCode48=47 if (sic4_acq>=6799&sic4_acq<=6799)
	replace acq_indCode48=48 if (sic4_acq>=4950&sic4_acq<=4959)
	replace acq_indCode48=48 if (sic4_acq>=4960&sic4_acq<=4961)
	replace acq_indCode48=48 if (sic4_acq>=4970&sic4_acq<=4971)
	replace acq_indCode48=48 if (sic4_acq>=4990&sic4_acq<=4991)
	label define acq_indCode48Names 1 "Agriculture" 2 "Food Products" 3 "Candy & Soda" 4 "Beer & Liquor" 5 "Tobacco Products" 6 "Recreation" 7 "Entertainment" 8 "Printing and Publishing" 9 "Consumer Goods" 10 "Apparel" 11 "Healthcare" 12 "Medical Equipment" 13 "Pharmaceutical Products" 14 "Chemicals" 15 "Rubber and Plastic Products" 16 "Textiles" 17 "Construction Materials" 18 "Construction" 19 "Steel Works Etc" 20 "Fabricated Products" 21 "Machinery" 22 "Electrical Equipment" 23 "Automobiles and Trucks" 24 "Aircraft" 25 "Shipbuilding, Railroad Equipment" 26 "Defense" 27 "Precious Metals" 28 "Non-Metallic and Industrial Metal Mining" 29 "Coal" 30 "Petroleum and Natural Gas" 31 "Utilities" 32 "Communication" 33 "Personal Services" 34 "Business Services" 35 "Computers" 36 "Electronic Equipment" 37 "Measuring and Control Equipment" 38 "Business Supplies" 39 "Shipping Containers" 40 "Transportation" 41 "Wholesale" 42 "Retail" 43 "Restaraunts, Hotels, Motels" 44 "Banking" 45 "Insurance" 46 "Real Estate" 47 "Trading" 48 "Almost Nothing"
	label values acq_indCode48 acq_indCode48Names
	gen sameInd48=cond(trg_indCode48==acq_indCode48,1,0)
	gen sameInd12=cond(trg_indCode==acq_indCode,1,0)	
save sdcMnA_linked, replace


****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Additional Repurchases from Amt. Outstanding and history files
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************

clear
use amtOut_hist	
	keep if action_type=="IRP" | action_type=="T"
	bysort issue_id effective_date action_type action_price action_amount amount_outstanding: drop if _n>1	
	sort issue_id effective_date action_type transaction_id amount_outstanding 
	collapse (sum) action_amount (min) amount_outstanding (mean) action_price (count)noSmDayActions=transaction_id, by(issue_id effective_date action_type)
	rename * *_hist
	rename issue_id_hist issue_id 
save amtOut_hist_agg, replace


****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Identify rating change timeline - could be used to study correlation with buybacks
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************


* Rating on Issue Date
clear
use fisdRatings
	merge m:1 issue_id using fisdIssue, keepusing(offering_date)
	keep if _merge==3
	drop _merge
	gen ratingClose2Offer= rating_date-offering_date
	keep if ratingClose2Offer<=30	
	drop ratingClose2Offer
	*keep if rating_date==offering_date
	sort issue_id rating_type rating_date
	by issue_id rating_type: drop if _n>1
	egen ratingType=group(rating_type)
tempfile ratings
save `ratings'
	*drop rating_type rating_status reason rating_status_date investment_grade 
	*reshape wide rating_type rating_status reason rating_status_date investment_grade, i(issue_id issuer_id) j(ratingType)
	reshape wide rating rating_type rating_status reason rating_status_date investment_grade rating_date, i(issue_id) j(ratingType)
	rename (rating1 rating2 rating3 rating4) (ratingDPR ratingFR ratingMR ratingSPR)
save fisdInitalRating, replace	
	
* Rating on change date	
clear
use issue_id effective_date action_type using fisdIssue
	duplicates drop 
tempfile fisdChanges	
save `fisdChanges'

* Additional repurchases from changes history
clear
use issue_id effective_date action_type using amtOut_hist_agg
	rename *_hist *
	duplicates drop
	append using `fisdChanges'
	duplicates drop
tempfile totalChanges	
save `totalChanges'

clear
use fisdRatings
	*merge m:1 issue_id using `totalChanges', keepusing(effective_date action_type)
	joinby issue_id using `totalChanges', unm(b)
	keep if _merge == 3 
	drop _merge
	gen ratingClose2Change= rating_date-effective_date
	keep if ratingClose2Change<=0
	drop ratingClose2Change
	sort issue_id effective_date action_type rating_type rating_date
	by issue_id effective_date action_type rating_type: keep if _n==_N | _n==_N-1 | _n==_N-2
	by issue_id effective_date action_type rating_type: gen chngRateOrdr=rating_type+string((_N-_n)+1)
	egen ratingOrder=group(chngRateOrdr)
	reshape wide rating rating_type rating_date chngRateOrdr rating_status reason rating_status_date investment_grade , i(issue_id effective_date action_type) j(ratingOrder)
	rename (rating1 rating2 rating3 rating4 rating5 rating6 rating7 rating8 rating9 rating10 rating11 rating12) (ratingDPR_close1 ratingDPR_close2 ratingDPR_close3 ratingFR_close1 ratingFR_close2 ratingFR_close3 ratingMR_close1 ratingMR_close2 ratingMR_close3 ratingSPR_close1 ratingSPR_close2 ratingSPR_close3)
save fisdChangeRating, replace	

****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Sample selection: 
*					1. USA domiciled
*					2. Include only Corporate Bonds	- exclude Government, Preferred stocks, foreign, Yankee, Canadian bonds, munis, etc
*					3. Exclude bonds issued before 1990
*					4. Exclude bonds issued before 2002 and have a maturity before Jan 2002
*					4. Merge with CRSP & Compustat
*					5. Identify repurchases and tender offer
*					6. Combine time of issue  Bond ratings and rating around change/action
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
clear
use crspMonthlyLink
	 keep permco gvkey yrMth
	 duplicates drop permco yrMth, force
tempfile gvkeyFile
save `gvkeyFile'
clear
use permco gvkey yr issuer_cusip_head using compuAnnualLink
	duplicates drop gvkey yr, force
tempfile compuLink	
save `compuLink'
clear
use fisdIssuer
	gen issuerCntry=cond(country_domicile!="",country_domicile, country)
tempfile fisdIssuer_cntry
save `fisdIssuer_cntry'
clear
use fisdIssue
	******************************
	egen event=tag(issue_id)
	egen issue=tag(issue_id)
	egen issuer=tag(issuer_id)
	egen fisdEvent=sum(event)
	egen fisdIssue=sum(issue)
	egen fisdIssuer=sum(issuer)
	drop event issue issuer
	******************************
	merge 1:1 issue_id using fisdRedemption, keepusing(callable make_whole sinking_fund)
	drop if _merge == 2
	drop _merge
	merge m:1 issuer_id using `fisdIssuer_cntry', keepusing(issuerCntry sic_code industry_group industry_code naics_code)
	drop if _merge == 2
	drop _merge
	keep if issuerCntry=="USA"
	******************************
	egen event=tag(issue_id)
	egen issue=tag(issue_id)
	egen issuer=tag(issuer_id)
	egen usaEvent=sum(event)
	egen usaIssue=sum(issue)
	egen usaIssuer=sum(issuer)
	drop event issue issuer
	******************************
	keep if bond_type=="CCOV" | bond_type=="CCPI" | bond_type=="CDEB" | bond_type=="CLOC" | bond_type=="CP" | bond_type=="CPAS" | bond_type=="CPIK" |  bond_type=="CS" | bond_type=="CUIT" | bond_type=="CZ" | bond_type=="UCID" | bond_type=="USBN"  | bond_type=="PS" | bond_type=="RN" 
	* |  bond_type=="CMTN" | bond_type=="CMTZ" 
	******************************
	egen event=tag(issue_id)
	egen issue=tag(issue_id)
	egen issuer=tag(issuer_id)
	egen corpEvent=sum(event)
	egen corpIssue=sum(issue)
	egen corpIssuer=sum(issuer)
	drop event issue issuer
	******************************
	
	drop if yankee=="Y" | canadian=="Y" | rule_144a=="Y" | foreign_currency=="Y" | putable=="Y"	| asset_backed=="Y" | sinking_fund=="Y"
	
	******************************
	egen event=tag(issue_id)
	egen issue=tag(issue_id)
	egen issuer=tag(issuer_id)
	egen optEvent=sum(event)
	egen optIssue=sum(issue)
	egen optIssuer=sum(issuer)
	drop event issue issuer
	******************************
	
	gen yrMth=ym(year(offering_date), month(offering_date))
	gen yr=year(offering_date)
	gen mYr=year(maturity)
	drop if yr<1990
	drop if mYr<2002 & yr<2002

	******************************
	egen event=tag(issue_id)
	egen issue=tag(issue_id)
	egen issuer=tag(issuer_id)
	egen aft90Event=sum(event)
	egen aft90Issue=sum(issue)
	egen aft90Issuer=sum(issuer)
	drop event issue issuer
	******************************

	gen secured=cond(security_level=="SS",1,0)
	* Industry classification
	gen sic4 = real(sic_code)
	* French 12 industries
	gen indCode=12	
	replace indCode=1 if (sic4>=100&sic4<=999)
	replace indCode=1 if (sic4>=2000&sic4<=2399)
	replace indCode=1 if (sic4>=2700&sic4<=2749)
	replace indCode=1 if (sic4>=2770&sic4<=2799)
	replace indCode=1 if (sic4>=3100&sic4<=3199)
	replace indCode=1 if (sic4>=3940&sic4<=3989)
	replace indCode=2 if (sic4>=2500&sic4<=2519)
	replace indCode=2 if (sic4>=2590&sic4<=2599)
	replace indCode=2 if (sic4>=3630&sic4<=3659)
	replace indCode=2 if (sic4>=3710&sic4<=3711)
	replace indCode=2 if (sic4>=3714&sic4<=3714)
	replace indCode=2 if (sic4>=3716&sic4<=3716)
	replace indCode=2 if (sic4>=3750&sic4<=3751)
	replace indCode=2 if (sic4>=3792&sic4<=3792)
	replace indCode=2 if (sic4>=3900&sic4<=3939)
	replace indCode=2 if (sic4>=3990&sic4<=3999)
	replace indCode=3 if (sic4>=2520&sic4<=2589)
	replace indCode=3 if (sic4>=2600&sic4<=2699)
	replace indCode=3 if (sic4>=2750&sic4<=2769)
	replace indCode=3 if (sic4>=3000&sic4<=3099)
	replace indCode=3 if (sic4>=3200&sic4<=3569)
	replace indCode=3 if (sic4>=3580&sic4<=3629)
	replace indCode=3 if (sic4>=3700&sic4<=3709)
	replace indCode=3 if (sic4>=3712&sic4<=3713)
	replace indCode=3 if (sic4>=3715&sic4<=3715)
	replace indCode=3 if (sic4>=3717&sic4<=3749)
	replace indCode=3 if (sic4>=3752&sic4<=3791)
	replace indCode=3 if (sic4>=3793&sic4<=3799)
	replace indCode=3 if (sic4>=3830&sic4<=3839)
	replace indCode=3 if (sic4>=3860&sic4<=3899)
	replace indCode=4 if (sic4>=1200&sic4<=1399)
	replace indCode=4 if (sic4>=2900&sic4<=2999)
	replace indCode=5 if (sic4>=2800&sic4<=2829)
	replace indCode=5 if (sic4>=2840&sic4<=2899)
	replace indCode=6 if (sic4>=3570&sic4<=3579)
	replace indCode=6 if (sic4>=3660&sic4<=3692)
	replace indCode=6 if (sic4>=3694&sic4<=3699)
	replace indCode=6 if (sic4>=3810&sic4<=3829)
	replace indCode=6 if (sic4>=7370&sic4<=7379)
	replace indCode=7 if (sic4>=4800&sic4<=4899)
	replace indCode=8 if (sic4>=4900&sic4<=4949)
	replace indCode=9 if (sic4>=5000&sic4<=5999)
	replace indCode=9 if (sic4>=7200&sic4<=7299)
	replace indCode=9 if (sic4>=7600&sic4<=7699)
	replace indCode=10 if (sic4>=2830&sic4<=2839)
	replace indCode=10 if (sic4>=3693&sic4<=3693)
	replace indCode=10 if (sic4>=3840&sic4<=3859)
	replace indCode=10 if (sic4>=8000&sic4<=8099)
	replace indCode=11 if (sic4>=6000&sic4<=6999)
	label define indNames 1 "Non Durables" 2 "Durables" 3 "Manufacturing" 4 "Energy" 5 "Chemicals" 6 "Business Equipment" 7 "Telecoms" 8 "Utilities" 9 "Wholesales" 10 "Healthcare" 11 "Finance" 12 "Others"
	label values indCode indNames
	
	* French 48 industries
	gen indCode48=48	
	replace indCode48=1 if (sic4>=100&sic4<=199)
	replace indCode48=1 if (sic4>=200&sic4<=299)
	replace indCode48=1 if (sic4>=700&sic4<=799)
	replace indCode48=1 if (sic4>=910&sic4<=919)
	replace indCode48=1 if (sic4>=2048&sic4<=2048)
	replace indCode48=2 if (sic4>=2000&sic4<=2009)
	replace indCode48=2 if (sic4>=2010&sic4<=2019)
	replace indCode48=2 if (sic4>=2020&sic4<=2029)
	replace indCode48=2 if (sic4>=2030&sic4<=2039)
	replace indCode48=2 if (sic4>=2040&sic4<=2046)
	replace indCode48=2 if (sic4>=2050&sic4<=2059)
	replace indCode48=2 if (sic4>=2060&sic4<=2063)
	replace indCode48=2 if (sic4>=2070&sic4<=2079)
	replace indCode48=2 if (sic4>=2090&sic4<=2092)
	replace indCode48=2 if (sic4>=2095&sic4<=2095)
	replace indCode48=2 if (sic4>=2098&sic4<=2099)
	replace indCode48=3 if (sic4>=2064&sic4<=2068)
	replace indCode48=3 if (sic4>=2086&sic4<=2086)
	replace indCode48=3 if (sic4>=2087&sic4<=2087)
	replace indCode48=3 if (sic4>=2096&sic4<=2096)
	replace indCode48=3 if (sic4>=2097&sic4<=2097)
	replace indCode48=4 if (sic4>=2080&sic4<=2080)
	replace indCode48=4 if (sic4>=2082&sic4<=2082)
	replace indCode48=4 if (sic4>=2083&sic4<=2083)
	replace indCode48=4 if (sic4>=2084&sic4<=2084)
	replace indCode48=4 if (sic4>=2085&sic4<=2085)
	replace indCode48=5 if (sic4>=2100&sic4<=2199)
	replace indCode48=6 if (sic4>=920&sic4<=999)
	replace indCode48=6 if (sic4>=3650&sic4<=3651)
	replace indCode48=6 if (sic4>=3652&sic4<=3652)
	replace indCode48=6 if (sic4>=3732&sic4<=3732)
	replace indCode48=6 if (sic4>=3930&sic4<=3931)
	replace indCode48=6 if (sic4>=3940&sic4<=3949)
	replace indCode48=7 if (sic4>=7800&sic4<=7829)
	replace indCode48=7 if (sic4>=7830&sic4<=7833)
	replace indCode48=7 if (sic4>=7840&sic4<=7841)
	replace indCode48=7 if (sic4>=7900&sic4<=7900)
	replace indCode48=7 if (sic4>=7910&sic4<=7911)
	replace indCode48=7 if (sic4>=7920&sic4<=7929)
	replace indCode48=7 if (sic4>=7930&sic4<=7933)
	replace indCode48=7 if (sic4>=7940&sic4<=7949)
	replace indCode48=7 if (sic4>=7980&sic4<=7980)
	replace indCode48=7 if (sic4>=7990&sic4<=7999)
	replace indCode48=8 if (sic4>=2700&sic4<=2709)
	replace indCode48=8 if (sic4>=2710&sic4<=2719)
	replace indCode48=8 if (sic4>=2720&sic4<=2729)
	replace indCode48=8 if (sic4>=2730&sic4<=2739)
	replace indCode48=8 if (sic4>=2740&sic4<=2749)
	replace indCode48=8 if (sic4>=2770&sic4<=2771)
	replace indCode48=8 if (sic4>=2780&sic4<=2789)
	replace indCode48=8 if (sic4>=2790&sic4<=2799)
	replace indCode48=9 if (sic4>=2047&sic4<=2047)
	replace indCode48=9 if (sic4>=2391&sic4<=2392)
	replace indCode48=9 if (sic4>=2510&sic4<=2519)
	replace indCode48=9 if (sic4>=2590&sic4<=2599)
	replace indCode48=9 if (sic4>=2840&sic4<=2843)
	replace indCode48=9 if (sic4>=2844&sic4<=2844)
	replace indCode48=9 if (sic4>=3160&sic4<=3161)
	replace indCode48=9 if (sic4>=3170&sic4<=3171)
	replace indCode48=9 if (sic4>=3172&sic4<=3172)
	replace indCode48=9 if (sic4>=3190&sic4<=3199)
	replace indCode48=9 if (sic4>=3229&sic4<=3229)
	replace indCode48=9 if (sic4>=3260&sic4<=3260)
	replace indCode48=9 if (sic4>=3262&sic4<=3263)
	replace indCode48=9 if (sic4>=3269&sic4<=3269)
	replace indCode48=9 if (sic4>=3230&sic4<=3231)
	replace indCode48=9 if (sic4>=3630&sic4<=3639)
	replace indCode48=9 if (sic4>=3750&sic4<=3751)
	replace indCode48=9 if (sic4>=3800&sic4<=3800)
	replace indCode48=9 if (sic4>=3860&sic4<=3861)
	replace indCode48=9 if (sic4>=3870&sic4<=3873)
	replace indCode48=9 if (sic4>=3910&sic4<=3911)
	replace indCode48=9 if (sic4>=3914&sic4<=3914)
	replace indCode48=9 if (sic4>=3915&sic4<=3915)
	replace indCode48=9 if (sic4>=3960&sic4<=3962)
	replace indCode48=9 if (sic4>=3991&sic4<=3991)
	replace indCode48=9 if (sic4>=3995&sic4<=3995)
	replace indCode48=10 if (sic4>=2300&sic4<=2390)
	replace indCode48=10 if (sic4>=3020&sic4<=3021)
	replace indCode48=10 if (sic4>=3100&sic4<=3111)
	replace indCode48=10 if (sic4>=3130&sic4<=3131)
	replace indCode48=10 if (sic4>=3140&sic4<=3149)
	replace indCode48=10 if (sic4>=3150&sic4<=3151)
	replace indCode48=10 if (sic4>=3963&sic4<=3965)
	replace indCode48=11 if (sic4>=8000&sic4<=8099)
	replace indCode48=12 if (sic4>=3693&sic4<=3693)
	replace indCode48=12 if (sic4>=3840&sic4<=3849)
	replace indCode48=12 if (sic4>=3850&sic4<=3851)
	replace indCode48=13 if (sic4>=2830&sic4<=2830)
	replace indCode48=13 if (sic4>=2831&sic4<=2831)
	replace indCode48=13 if (sic4>=2833&sic4<=2833)
	replace indCode48=13 if (sic4>=2834&sic4<=2834)
	replace indCode48=13 if (sic4>=2835&sic4<=2835)
	replace indCode48=13 if (sic4>=2836&sic4<=2836)
	replace indCode48=14 if (sic4>=2800&sic4<=2809)
	replace indCode48=14 if (sic4>=2810&sic4<=2819)
	replace indCode48=14 if (sic4>=2820&sic4<=2829)
	replace indCode48=14 if (sic4>=2850&sic4<=2859)
	replace indCode48=14 if (sic4>=2860&sic4<=2869)
	replace indCode48=14 if (sic4>=2870&sic4<=2879)
	replace indCode48=14 if (sic4>=2890&sic4<=2899)
	replace indCode48=15 if (sic4>=3031&sic4<=3031)
	replace indCode48=15 if (sic4>=3041&sic4<=3041)
	replace indCode48=15 if (sic4>=3050&sic4<=3053)
	replace indCode48=15 if (sic4>=3060&sic4<=3069)
	replace indCode48=15 if (sic4>=3070&sic4<=3079)
	replace indCode48=15 if (sic4>=3080&sic4<=3089)
	replace indCode48=15 if (sic4>=3090&sic4<=3099)
	replace indCode48=16 if (sic4>=2200&sic4<=2269)
	replace indCode48=16 if (sic4>=2270&sic4<=2279)
	replace indCode48=16 if (sic4>=2280&sic4<=2284)
	replace indCode48=16 if (sic4>=2290&sic4<=2295)
	replace indCode48=16 if (sic4>=2297&sic4<=2297)
	replace indCode48=16 if (sic4>=2298&sic4<=2298)
	replace indCode48=16 if (sic4>=2299&sic4<=2299)
	replace indCode48=16 if (sic4>=2393&sic4<=2395)
	replace indCode48=16 if (sic4>=2397&sic4<=2399)
	replace indCode48=17 if (sic4>=800&sic4<=899)
	replace indCode48=17 if (sic4>=2400&sic4<=2439)
	replace indCode48=17 if (sic4>=2450&sic4<=2459)
	replace indCode48=17 if (sic4>=2490&sic4<=2499)
	replace indCode48=17 if (sic4>=2660&sic4<=2661)
	replace indCode48=17 if (sic4>=2950&sic4<=2952)
	replace indCode48=17 if (sic4>=3200&sic4<=3200)
	replace indCode48=17 if (sic4>=3210&sic4<=3211)
	replace indCode48=17 if (sic4>=3240&sic4<=3241)
	replace indCode48=17 if (sic4>=3250&sic4<=3259)
	replace indCode48=17 if (sic4>=3261&sic4<=3261)
	replace indCode48=17 if (sic4>=3264&sic4<=3264)
	replace indCode48=17 if (sic4>=3270&sic4<=3275)
	replace indCode48=17 if (sic4>=3280&sic4<=3281)
	replace indCode48=17 if (sic4>=3290&sic4<=3293)
	replace indCode48=17 if (sic4>=3295&sic4<=3299)
	replace indCode48=17 if (sic4>=3420&sic4<=3429)
	replace indCode48=17 if (sic4>=3430&sic4<=3433)
	replace indCode48=17 if (sic4>=3440&sic4<=3441)
	replace indCode48=17 if (sic4>=3442&sic4<=3442)
	replace indCode48=17 if (sic4>=3446&sic4<=3446)
	replace indCode48=17 if (sic4>=3448&sic4<=3448)
	replace indCode48=17 if (sic4>=3449&sic4<=3449)
	replace indCode48=17 if (sic4>=3450&sic4<=3451)
	replace indCode48=17 if (sic4>=3452&sic4<=3452)
	replace indCode48=17 if (sic4>=3490&sic4<=3499)
	replace indCode48=17 if (sic4>=3996&sic4<=3996)
	replace indCode48=18 if (sic4>=1500&sic4<=1511)
	replace indCode48=18 if (sic4>=1520&sic4<=1529)
	replace indCode48=18 if (sic4>=1530&sic4<=1539)
	replace indCode48=18 if (sic4>=1540&sic4<=1549)
	replace indCode48=18 if (sic4>=1600&sic4<=1699)
	replace indCode48=18 if (sic4>=1700&sic4<=1799)
	replace indCode48=19 if (sic4>=3300&sic4<=3300)
	replace indCode48=19 if (sic4>=3310&sic4<=3317)
	replace indCode48=19 if (sic4>=3320&sic4<=3325)
	replace indCode48=19 if (sic4>=3330&sic4<=3339)
	replace indCode48=19 if (sic4>=3340&sic4<=3341)
	replace indCode48=19 if (sic4>=3350&sic4<=3357)
	replace indCode48=19 if (sic4>=3360&sic4<=3369)
	replace indCode48=19 if (sic4>=3370&sic4<=3379)
	replace indCode48=19 if (sic4>=3390&sic4<=3399)
	replace indCode48=20 if (sic4>=3400&sic4<=3400)
	replace indCode48=20 if (sic4>=3443&sic4<=3443)
	replace indCode48=20 if (sic4>=3444&sic4<=3444)
	replace indCode48=20 if (sic4>=3460&sic4<=3469)
	replace indCode48=20 if (sic4>=3470&sic4<=3479)
	replace indCode48=21 if (sic4>=3510&sic4<=3519)
	replace indCode48=21 if (sic4>=3520&sic4<=3529)
	replace indCode48=21 if (sic4>=3530&sic4<=3530)
	replace indCode48=21 if (sic4>=3531&sic4<=3531)
	replace indCode48=21 if (sic4>=3532&sic4<=3532)
	replace indCode48=21 if (sic4>=3533&sic4<=3533)
	replace indCode48=21 if (sic4>=3534&sic4<=3534)
	replace indCode48=21 if (sic4>=3535&sic4<=3535)
	replace indCode48=21 if (sic4>=3536&sic4<=3536)
	replace indCode48=21 if (sic4>=3538&sic4<=3538)
	replace indCode48=21 if (sic4>=3540&sic4<=3549)
	replace indCode48=21 if (sic4>=3550&sic4<=3559)
	replace indCode48=21 if (sic4>=3560&sic4<=3569)
	replace indCode48=21 if (sic4>=3580&sic4<=3580)
	replace indCode48=21 if (sic4>=3581&sic4<=3581)
	replace indCode48=21 if (sic4>=3582&sic4<=3582)
	replace indCode48=21 if (sic4>=3585&sic4<=3585)
	replace indCode48=21 if (sic4>=3586&sic4<=3586)
	replace indCode48=21 if (sic4>=3589&sic4<=3589)
	replace indCode48=21 if (sic4>=3590&sic4<=3599)
	replace indCode48=22 if (sic4>=3600&sic4<=3600)
	replace indCode48=22 if (sic4>=3610&sic4<=3613)
	replace indCode48=22 if (sic4>=3620&sic4<=3621)
	replace indCode48=22 if (sic4>=3623&sic4<=3629)
	replace indCode48=22 if (sic4>=3640&sic4<=3644)
	replace indCode48=22 if (sic4>=3645&sic4<=3645)
	replace indCode48=22 if (sic4>=3646&sic4<=3646)
	replace indCode48=22 if (sic4>=3648&sic4<=3649)
	replace indCode48=22 if (sic4>=3660&sic4<=3660)
	replace indCode48=22 if (sic4>=3690&sic4<=3690)
	replace indCode48=22 if (sic4>=3691&sic4<=3692)
	replace indCode48=22 if (sic4>=3699&sic4<=3699)
	replace indCode48=23 if (sic4>=2296&sic4<=2296)
	replace indCode48=23 if (sic4>=2396&sic4<=2396)
	replace indCode48=23 if (sic4>=3010&sic4<=3011)
	replace indCode48=23 if (sic4>=3537&sic4<=3537)
	replace indCode48=23 if (sic4>=3647&sic4<=3647)
	replace indCode48=23 if (sic4>=3694&sic4<=3694)
	replace indCode48=23 if (sic4>=3700&sic4<=3700)
	replace indCode48=23 if (sic4>=3710&sic4<=3710)
	replace indCode48=23 if (sic4>=3711&sic4<=3711)
	replace indCode48=23 if (sic4>=3713&sic4<=3713)
	replace indCode48=23 if (sic4>=3714&sic4<=3714)
	replace indCode48=23 if (sic4>=3715&sic4<=3715)
	replace indCode48=23 if (sic4>=3716&sic4<=3716)
	replace indCode48=23 if (sic4>=3792&sic4<=3792)
	replace indCode48=23 if (sic4>=3790&sic4<=3791)
	replace indCode48=23 if (sic4>=3799&sic4<=3799)
	replace indCode48=24 if (sic4>=3720&sic4<=3720)
	replace indCode48=24 if (sic4>=3721&sic4<=3721)
	replace indCode48=24 if (sic4>=3723&sic4<=3724)
	replace indCode48=24 if (sic4>=3725&sic4<=3725)
	replace indCode48=24 if (sic4>=3728&sic4<=3729)
	replace indCode48=25 if (sic4>=3730&sic4<=3731)
	replace indCode48=25 if (sic4>=3740&sic4<=3743)
	replace indCode48=26 if (sic4>=3760&sic4<=3769)
	replace indCode48=26 if (sic4>=3795&sic4<=3795)
	replace indCode48=26 if (sic4>=3480&sic4<=3489)
	replace indCode48=27 if (sic4>=1040&sic4<=1049)
	replace indCode48=28 if (sic4>=1000&sic4<=1009)
	replace indCode48=28 if (sic4>=1010&sic4<=1019)
	replace indCode48=28 if (sic4>=1020&sic4<=1029)
	replace indCode48=28 if (sic4>=1030&sic4<=1039)
	replace indCode48=28 if (sic4>=1050&sic4<=1059)
	replace indCode48=28 if (sic4>=1060&sic4<=1069)
	replace indCode48=28 if (sic4>=1070&sic4<=1079)
	replace indCode48=28 if (sic4>=1080&sic4<=1089)
	replace indCode48=28 if (sic4>=1090&sic4<=1099)
	replace indCode48=28 if (sic4>=1100&sic4<=1119)
	replace indCode48=28 if (sic4>=1400&sic4<=1499)
	replace indCode48=29 if (sic4>=1200&sic4<=1299)
	replace indCode48=30 if (sic4>=1300&sic4<=1300)
	replace indCode48=30 if (sic4>=1310&sic4<=1319)
	replace indCode48=30 if (sic4>=1320&sic4<=1329)
	replace indCode48=30 if (sic4>=1330&sic4<=1339)
	replace indCode48=30 if (sic4>=1370&sic4<=1379)
	replace indCode48=30 if (sic4>=1380&sic4<=1380)
	replace indCode48=30 if (sic4>=1381&sic4<=1381)
	replace indCode48=30 if (sic4>=1382&sic4<=1382)
	replace indCode48=30 if (sic4>=1389&sic4<=1389)
	replace indCode48=30 if (sic4>=2900&sic4<=2912)
	replace indCode48=30 if (sic4>=2990&sic4<=2999)
	replace indCode48=31 if (sic4>=4900&sic4<=4900)
	replace indCode48=31 if (sic4>=4910&sic4<=4911)
	replace indCode48=31 if (sic4>=4920&sic4<=4922)
	replace indCode48=31 if (sic4>=4923&sic4<=4923)
	replace indCode48=31 if (sic4>=4924&sic4<=4925)
	replace indCode48=31 if (sic4>=4930&sic4<=4931)
	replace indCode48=31 if (sic4>=4932&sic4<=4932)
	replace indCode48=31 if (sic4>=4939&sic4<=4939)
	replace indCode48=31 if (sic4>=4940&sic4<=4942)
	replace indCode48=32 if (sic4>=4800&sic4<=4800)
	replace indCode48=32 if (sic4>=4810&sic4<=4813)
	replace indCode48=32 if (sic4>=4820&sic4<=4822)
	replace indCode48=32 if (sic4>=4830&sic4<=4839)
	replace indCode48=32 if (sic4>=4840&sic4<=4841)
	replace indCode48=32 if (sic4>=4880&sic4<=4889)
	replace indCode48=32 if (sic4>=4890&sic4<=4890)
	replace indCode48=32 if (sic4>=4891&sic4<=4891)
	replace indCode48=32 if (sic4>=4892&sic4<=4892)
	replace indCode48=32 if (sic4>=4899&sic4<=4899)
	replace indCode48=33 if (sic4>=7020&sic4<=7021)
	replace indCode48=33 if (sic4>=7030&sic4<=7033)
	replace indCode48=33 if (sic4>=7200&sic4<=7200)
	replace indCode48=33 if (sic4>=7210&sic4<=7212)
	replace indCode48=33 if (sic4>=7214&sic4<=7214)
	replace indCode48=33 if (sic4>=7215&sic4<=7216)
	replace indCode48=33 if (sic4>=7217&sic4<=7217)
	replace indCode48=33 if (sic4>=7219&sic4<=7219)
	replace indCode48=33 if (sic4>=7220&sic4<=7221)
	replace indCode48=33 if (sic4>=7230&sic4<=7231)
	replace indCode48=33 if (sic4>=7240&sic4<=7241)
	replace indCode48=33 if (sic4>=7250&sic4<=7251)
	replace indCode48=33 if (sic4>=7260&sic4<=7269)
	replace indCode48=33 if (sic4>=7270&sic4<=7290)
	replace indCode48=33 if (sic4>=7291&sic4<=7291)
	replace indCode48=33 if (sic4>=7292&sic4<=7299)
	replace indCode48=33 if (sic4>=7395&sic4<=7395)
	replace indCode48=33 if (sic4>=7500&sic4<=7500)
	replace indCode48=33 if (sic4>=7520&sic4<=7529)
	replace indCode48=33 if (sic4>=7530&sic4<=7539)
	replace indCode48=33 if (sic4>=7540&sic4<=7549)
	replace indCode48=33 if (sic4>=7600&sic4<=7600)
	replace indCode48=33 if (sic4>=7620&sic4<=7620)
	replace indCode48=33 if (sic4>=7622&sic4<=7622)
	replace indCode48=33 if (sic4>=7623&sic4<=7623)
	replace indCode48=33 if (sic4>=7629&sic4<=7629)
	replace indCode48=33 if (sic4>=7630&sic4<=7631)
	replace indCode48=33 if (sic4>=7640&sic4<=7641)
	replace indCode48=33 if (sic4>=7690&sic4<=7699)
	replace indCode48=33 if (sic4>=8100&sic4<=8199)
	replace indCode48=33 if (sic4>=8200&sic4<=8299)
	replace indCode48=33 if (sic4>=8300&sic4<=8399)
	replace indCode48=33 if (sic4>=8400&sic4<=8499)
	replace indCode48=33 if (sic4>=8600&sic4<=8699)
	replace indCode48=33 if (sic4>=8800&sic4<=8899)
	replace indCode48=33 if (sic4>=7510&sic4<=7515)
	replace indCode48=34 if (sic4>=2750&sic4<=2759)
	replace indCode48=34 if (sic4>=3993&sic4<=3993)
	replace indCode48=34 if (sic4>=7218&sic4<=7218)
	replace indCode48=34 if (sic4>=7300&sic4<=7300)
	replace indCode48=34 if (sic4>=7310&sic4<=7319)
	replace indCode48=34 if (sic4>=7320&sic4<=7329)
	replace indCode48=34 if (sic4>=7330&sic4<=7339)
	replace indCode48=34 if (sic4>=7340&sic4<=7342)
	replace indCode48=34 if (sic4>=7349&sic4<=7349)
	replace indCode48=34 if (sic4>=7350&sic4<=7351)
	replace indCode48=34 if (sic4>=7352&sic4<=7352)
	replace indCode48=34 if (sic4>=7353&sic4<=7353)
	replace indCode48=34 if (sic4>=7359&sic4<=7359)
	replace indCode48=34 if (sic4>=7360&sic4<=7369)
	replace indCode48=34 if (sic4>=7370&sic4<=7372)
	replace indCode48=34 if (sic4>=7374&sic4<=7374)
	replace indCode48=34 if (sic4>=7375&sic4<=7375)
	replace indCode48=34 if (sic4>=7376&sic4<=7376)
	replace indCode48=34 if (sic4>=7377&sic4<=7377)
	replace indCode48=34 if (sic4>=7378&sic4<=7378)
	replace indCode48=34 if (sic4>=7379&sic4<=7379)
	replace indCode48=34 if (sic4>=7380&sic4<=7380)
	replace indCode48=34 if (sic4>=7381&sic4<=7382)
	replace indCode48=34 if (sic4>=7383&sic4<=7383)
	replace indCode48=34 if (sic4>=7384&sic4<=7384)
	replace indCode48=34 if (sic4>=7385&sic4<=7385)
	replace indCode48=34 if (sic4>=7389&sic4<=7390)
	replace indCode48=34 if (sic4>=7391&sic4<=7391)
	replace indCode48=34 if (sic4>=7392&sic4<=7392)
	replace indCode48=34 if (sic4>=7393&sic4<=7393)
	replace indCode48=34 if (sic4>=7394&sic4<=7394)
	replace indCode48=34 if (sic4>=7396&sic4<=7396)
	replace indCode48=34 if (sic4>=7397&sic4<=7397)
	replace indCode48=34 if (sic4>=7399&sic4<=7399)
	replace indCode48=34 if (sic4>=7519&sic4<=7519)
	replace indCode48=34 if (sic4>=8700&sic4<=8700)
	replace indCode48=34 if (sic4>=8710&sic4<=8713)
	replace indCode48=34 if (sic4>=8720&sic4<=8721)
	replace indCode48=34 if (sic4>=8730&sic4<=8734)
	replace indCode48=34 if (sic4>=8740&sic4<=8748)
	replace indCode48=34 if (sic4>=8900&sic4<=8910)
	replace indCode48=34 if (sic4>=8911&sic4<=8911)
	replace indCode48=34 if (sic4>=8920&sic4<=8999)
	replace indCode48=34 if (sic4>=4220&sic4<=4229)
	replace indCode48=35 if (sic4>=3570&sic4<=3579)
	replace indCode48=35 if (sic4>=3680&sic4<=3680)
	replace indCode48=35 if (sic4>=3681&sic4<=3681)
	replace indCode48=35 if (sic4>=3682&sic4<=3682)
	replace indCode48=35 if (sic4>=3683&sic4<=3683)
	replace indCode48=35 if (sic4>=3684&sic4<=3684)
	replace indCode48=35 if (sic4>=3685&sic4<=3685)
	replace indCode48=35 if (sic4>=3686&sic4<=3686)
	replace indCode48=35 if (sic4>=3687&sic4<=3687)
	replace indCode48=35 if (sic4>=3688&sic4<=3688)
	replace indCode48=35 if (sic4>=3689&sic4<=3689)
	replace indCode48=35 if (sic4>=3695&sic4<=3695)
	replace indCode48=35 if (sic4>=7373&sic4<=7373)
	replace indCode48=36 if (sic4>=3622&sic4<=3622)
	replace indCode48=36 if (sic4>=3661&sic4<=3661)
	replace indCode48=36 if (sic4>=3662&sic4<=3662)
	replace indCode48=36 if (sic4>=3663&sic4<=3663)
	replace indCode48=36 if (sic4>=3664&sic4<=3664)
	replace indCode48=36 if (sic4>=3665&sic4<=3665)
	replace indCode48=36 if (sic4>=3666&sic4<=3666)
	replace indCode48=36 if (sic4>=3669&sic4<=3669)
	replace indCode48=36 if (sic4>=3670&sic4<=3679)
	replace indCode48=36 if (sic4>=3810&sic4<=3810)
	replace indCode48=36 if (sic4>=3812&sic4<=3812)
	replace indCode48=37 if (sic4>=3811&sic4<=3811)
	replace indCode48=37 if (sic4>=3820&sic4<=3820)
	replace indCode48=37 if (sic4>=3821&sic4<=3821)
	replace indCode48=37 if (sic4>=3822&sic4<=3822)
	replace indCode48=37 if (sic4>=3823&sic4<=3823)
	replace indCode48=37 if (sic4>=3824&sic4<=3824)
	replace indCode48=37 if (sic4>=3825&sic4<=3825)
	replace indCode48=37 if (sic4>=3826&sic4<=3826)
	replace indCode48=37 if (sic4>=3827&sic4<=3827)
	replace indCode48=37 if (sic4>=3829&sic4<=3829)
	replace indCode48=37 if (sic4>=3830&sic4<=3839)
	replace indCode48=38 if (sic4>=2520&sic4<=2549)
	replace indCode48=38 if (sic4>=2600&sic4<=2639)
	replace indCode48=38 if (sic4>=2670&sic4<=2699)
	replace indCode48=38 if (sic4>=2760&sic4<=2761)
	replace indCode48=38 if (sic4>=3950&sic4<=3955)
	replace indCode48=39 if (sic4>=2440&sic4<=2449)
	replace indCode48=39 if (sic4>=2640&sic4<=2659)
	replace indCode48=39 if (sic4>=3220&sic4<=3221)
	replace indCode48=39 if (sic4>=3410&sic4<=3412)
	replace indCode48=40 if (sic4>=4000&sic4<=4013)
	replace indCode48=40 if (sic4>=4040&sic4<=4049)
	replace indCode48=40 if (sic4>=4100&sic4<=4100)
	replace indCode48=40 if (sic4>=4110&sic4<=4119)
	replace indCode48=40 if (sic4>=4120&sic4<=4121)
	replace indCode48=40 if (sic4>=4130&sic4<=4131)
	replace indCode48=40 if (sic4>=4140&sic4<=4142)
	replace indCode48=40 if (sic4>=4150&sic4<=4151)
	replace indCode48=40 if (sic4>=4170&sic4<=4173)
	replace indCode48=40 if (sic4>=4190&sic4<=4199)
	replace indCode48=40 if (sic4>=4200&sic4<=4200)
	replace indCode48=40 if (sic4>=4210&sic4<=4219)
	replace indCode48=40 if (sic4>=4230&sic4<=4231)
	replace indCode48=40 if (sic4>=4240&sic4<=4249)
	replace indCode48=40 if (sic4>=4400&sic4<=4499)
	replace indCode48=40 if (sic4>=4500&sic4<=4599)
	replace indCode48=40 if (sic4>=4600&sic4<=4699)
	replace indCode48=40 if (sic4>=4700&sic4<=4700)
	replace indCode48=40 if (sic4>=4710&sic4<=4712)
	replace indCode48=40 if (sic4>=4720&sic4<=4729)
	replace indCode48=40 if (sic4>=4730&sic4<=4739)
	replace indCode48=40 if (sic4>=4740&sic4<=4749)
	replace indCode48=40 if (sic4>=4780&sic4<=4780)
	replace indCode48=40 if (sic4>=4782&sic4<=4782)
	replace indCode48=40 if (sic4>=4783&sic4<=4783)
	replace indCode48=40 if (sic4>=4784&sic4<=4784)
	replace indCode48=40 if (sic4>=4785&sic4<=4785)
	replace indCode48=40 if (sic4>=4789&sic4<=4789)
	replace indCode48=41 if (sic4>=5000&sic4<=5000)
	replace indCode48=41 if (sic4>=5010&sic4<=5015)
	replace indCode48=41 if (sic4>=5020&sic4<=5023)
	replace indCode48=41 if (sic4>=5030&sic4<=5039)
	replace indCode48=41 if (sic4>=5040&sic4<=5042)
	replace indCode48=41 if (sic4>=5043&sic4<=5043)
	replace indCode48=41 if (sic4>=5044&sic4<=5044)
	replace indCode48=41 if (sic4>=5045&sic4<=5045)
	replace indCode48=41 if (sic4>=5046&sic4<=5046)
	replace indCode48=41 if (sic4>=5047&sic4<=5047)
	replace indCode48=41 if (sic4>=5048&sic4<=5048)
	replace indCode48=41 if (sic4>=5049&sic4<=5049)
	replace indCode48=41 if (sic4>=5050&sic4<=5059)
	replace indCode48=41 if (sic4>=5060&sic4<=5060)
	replace indCode48=41 if (sic4>=5063&sic4<=5063)
	replace indCode48=41 if (sic4>=5064&sic4<=5064)
	replace indCode48=41 if (sic4>=5065&sic4<=5065)
	replace indCode48=41 if (sic4>=5070&sic4<=5078)
	replace indCode48=41 if (sic4>=5080&sic4<=5080)
	replace indCode48=41 if (sic4>=5081&sic4<=5081)
	replace indCode48=41 if (sic4>=5082&sic4<=5082)
	replace indCode48=41 if (sic4>=5083&sic4<=5083)
	replace indCode48=41 if (sic4>=5084&sic4<=5084)
	replace indCode48=41 if (sic4>=5085&sic4<=5085)
	replace indCode48=41 if (sic4>=5086&sic4<=5087)
	replace indCode48=41 if (sic4>=5088&sic4<=5088)
	replace indCode48=41 if (sic4>=5090&sic4<=5090)
	replace indCode48=41 if (sic4>=5091&sic4<=5092)
	replace indCode48=41 if (sic4>=5093&sic4<=5093)
	replace indCode48=41 if (sic4>=5094&sic4<=5094)
	replace indCode48=41 if (sic4>=5099&sic4<=5099)
	replace indCode48=41 if (sic4>=5100&sic4<=5100)
	replace indCode48=41 if (sic4>=5110&sic4<=5113)
	replace indCode48=41 if (sic4>=5120&sic4<=5122)
	replace indCode48=41 if (sic4>=5130&sic4<=5139)
	replace indCode48=41 if (sic4>=5140&sic4<=5149)
	replace indCode48=41 if (sic4>=5150&sic4<=5159)
	replace indCode48=41 if (sic4>=5160&sic4<=5169)
	replace indCode48=41 if (sic4>=5170&sic4<=5172)
	replace indCode48=41 if (sic4>=5180&sic4<=5182)
	replace indCode48=41 if (sic4>=5190&sic4<=5199)
	replace indCode48=42 if (sic4>=5200&sic4<=5200)
	replace indCode48=42 if (sic4>=5210&sic4<=5219)
	replace indCode48=42 if (sic4>=5220&sic4<=5229)
	replace indCode48=42 if (sic4>=5230&sic4<=5231)
	replace indCode48=42 if (sic4>=5250&sic4<=5251)
	replace indCode48=42 if (sic4>=5260&sic4<=5261)
	replace indCode48=42 if (sic4>=5270&sic4<=5271)
	replace indCode48=42 if (sic4>=5300&sic4<=5300)
	replace indCode48=42 if (sic4>=5310&sic4<=5311)
	replace indCode48=42 if (sic4>=5320&sic4<=5320)
	replace indCode48=42 if (sic4>=5330&sic4<=5331)
	replace indCode48=42 if (sic4>=5334&sic4<=5334)
	replace indCode48=42 if (sic4>=5340&sic4<=5349)
	replace indCode48=42 if (sic4>=5390&sic4<=5399)
	replace indCode48=42 if (sic4>=5400&sic4<=5400)
	replace indCode48=42 if (sic4>=5410&sic4<=5411)
	replace indCode48=42 if (sic4>=5412&sic4<=5412)
	replace indCode48=42 if (sic4>=5420&sic4<=5429)
	replace indCode48=42 if (sic4>=5430&sic4<=5439)
	replace indCode48=42 if (sic4>=5440&sic4<=5449)
	replace indCode48=42 if (sic4>=5450&sic4<=5459)
	replace indCode48=42 if (sic4>=5460&sic4<=5469)
	replace indCode48=42 if (sic4>=5490&sic4<=5499)
	replace indCode48=42 if (sic4>=5500&sic4<=5500)
	replace indCode48=42 if (sic4>=5510&sic4<=5529)
	replace indCode48=42 if (sic4>=5530&sic4<=5539)
	replace indCode48=42 if (sic4>=5540&sic4<=5549)
	replace indCode48=42 if (sic4>=5550&sic4<=5559)
	replace indCode48=42 if (sic4>=5560&sic4<=5569)
	replace indCode48=42 if (sic4>=5570&sic4<=5579)
	replace indCode48=42 if (sic4>=5590&sic4<=5599)
	replace indCode48=42 if (sic4>=5600&sic4<=5699)
	replace indCode48=42 if (sic4>=5700&sic4<=5700)
	replace indCode48=42 if (sic4>=5710&sic4<=5719)
	replace indCode48=42 if (sic4>=5720&sic4<=5722)
	replace indCode48=42 if (sic4>=5730&sic4<=5733)
	replace indCode48=42 if (sic4>=5734&sic4<=5734)
	replace indCode48=42 if (sic4>=5735&sic4<=5735)
	replace indCode48=42 if (sic4>=5736&sic4<=5736)
	replace indCode48=42 if (sic4>=5750&sic4<=5799)
	replace indCode48=42 if (sic4>=5900&sic4<=5900)
	replace indCode48=42 if (sic4>=5910&sic4<=5912)
	replace indCode48=42 if (sic4>=5920&sic4<=5929)
	replace indCode48=42 if (sic4>=5930&sic4<=5932)
	replace indCode48=42 if (sic4>=5940&sic4<=5940)
	replace indCode48=42 if (sic4>=5941&sic4<=5941)
	replace indCode48=42 if (sic4>=5942&sic4<=5942)
	replace indCode48=42 if (sic4>=5943&sic4<=5943)
	replace indCode48=42 if (sic4>=5944&sic4<=5944)
	replace indCode48=42 if (sic4>=5945&sic4<=5945)
	replace indCode48=42 if (sic4>=5946&sic4<=5946)
	replace indCode48=42 if (sic4>=5947&sic4<=5947)
	replace indCode48=42 if (sic4>=5948&sic4<=5948)
	replace indCode48=42 if (sic4>=5949&sic4<=5949)
	replace indCode48=42 if (sic4>=5950&sic4<=5959)
	replace indCode48=42 if (sic4>=5960&sic4<=5969)
	replace indCode48=42 if (sic4>=5970&sic4<=5979)
	replace indCode48=42 if (sic4>=5980&sic4<=5989)
	replace indCode48=42 if (sic4>=5990&sic4<=5990)
	replace indCode48=42 if (sic4>=5992&sic4<=5992)
	replace indCode48=42 if (sic4>=5993&sic4<=5993)
	replace indCode48=42 if (sic4>=5994&sic4<=5994)
	replace indCode48=42 if (sic4>=5995&sic4<=5995)
	replace indCode48=42 if (sic4>=5999&sic4<=5999)
	replace indCode48=43 if (sic4>=5800&sic4<=5819)
	replace indCode48=43 if (sic4>=5820&sic4<=5829)
	replace indCode48=43 if (sic4>=5890&sic4<=5899)
	replace indCode48=43 if (sic4>=7000&sic4<=7000)
	replace indCode48=43 if (sic4>=7010&sic4<=7019)
	replace indCode48=43 if (sic4>=7040&sic4<=7049)
	replace indCode48=43 if (sic4>=7213&sic4<=7213)
	replace indCode48=44 if (sic4>=6000&sic4<=6000)
	replace indCode48=44 if (sic4>=6010&sic4<=6019)
	replace indCode48=44 if (sic4>=6020&sic4<=6020)
	replace indCode48=44 if (sic4>=6021&sic4<=6021)
	replace indCode48=44 if (sic4>=6022&sic4<=6022)
	replace indCode48=44 if (sic4>=6023&sic4<=6024)
	replace indCode48=44 if (sic4>=6025&sic4<=6025)
	replace indCode48=44 if (sic4>=6026&sic4<=6026)
	replace indCode48=44 if (sic4>=6027&sic4<=6027)
	replace indCode48=44 if (sic4>=6028&sic4<=6029)
	replace indCode48=44 if (sic4>=6030&sic4<=6036)
	replace indCode48=44 if (sic4>=6040&sic4<=6059)
	replace indCode48=44 if (sic4>=6060&sic4<=6062)
	replace indCode48=44 if (sic4>=6080&sic4<=6082)
	replace indCode48=44 if (sic4>=6090&sic4<=6099)
	replace indCode48=44 if (sic4>=6100&sic4<=6100)
	replace indCode48=44 if (sic4>=6110&sic4<=6111)
	replace indCode48=44 if (sic4>=6112&sic4<=6113)
	replace indCode48=44 if (sic4>=6120&sic4<=6129)
	replace indCode48=44 if (sic4>=6130&sic4<=6139)
	replace indCode48=44 if (sic4>=6140&sic4<=6149)
	replace indCode48=44 if (sic4>=6150&sic4<=6159)
	replace indCode48=44 if (sic4>=6160&sic4<=6169)
	replace indCode48=44 if (sic4>=6170&sic4<=6179)
	replace indCode48=44 if (sic4>=6190&sic4<=6199)
	replace indCode48=45 if (sic4>=6300&sic4<=6300)
	replace indCode48=45 if (sic4>=6310&sic4<=6319)
	replace indCode48=45 if (sic4>=6320&sic4<=6329)
	replace indCode48=45 if (sic4>=6330&sic4<=6331)
	replace indCode48=45 if (sic4>=6350&sic4<=6351)
	replace indCode48=45 if (sic4>=6360&sic4<=6361)
	replace indCode48=45 if (sic4>=6370&sic4<=6379)
	replace indCode48=45 if (sic4>=6390&sic4<=6399)
	replace indCode48=45 if (sic4>=6400&sic4<=6411)
	replace indCode48=46 if (sic4>=6500&sic4<=6500)
	replace indCode48=46 if (sic4>=6510&sic4<=6510)
	replace indCode48=46 if (sic4>=6512&sic4<=6512)
	replace indCode48=46 if (sic4>=6513&sic4<=6513)
	replace indCode48=46 if (sic4>=6514&sic4<=6514)
	replace indCode48=46 if (sic4>=6515&sic4<=6515)
	replace indCode48=46 if (sic4>=6517&sic4<=6519)
	replace indCode48=46 if (sic4>=6520&sic4<=6529)
	replace indCode48=46 if (sic4>=6530&sic4<=6531)
	replace indCode48=46 if (sic4>=6532&sic4<=6532)
	replace indCode48=46 if (sic4>=6540&sic4<=6541)
	replace indCode48=46 if (sic4>=6550&sic4<=6553)
	replace indCode48=46 if (sic4>=6590&sic4<=6599)
	replace indCode48=46 if (sic4>=6610&sic4<=6611)
	replace indCode48=47 if (sic4>=6200&sic4<=6299)
	replace indCode48=47 if (sic4>=6700&sic4<=6700)
	replace indCode48=47 if (sic4>=6710&sic4<=6719)
	replace indCode48=47 if (sic4>=6720&sic4<=6722)
	replace indCode48=47 if (sic4>=6723&sic4<=6723)
	replace indCode48=47 if (sic4>=6724&sic4<=6724)
	replace indCode48=47 if (sic4>=6725&sic4<=6725)
	replace indCode48=47 if (sic4>=6726&sic4<=6726)
	replace indCode48=47 if (sic4>=6730&sic4<=6733)
	replace indCode48=47 if (sic4>=6740&sic4<=6779)
	replace indCode48=47 if (sic4>=6790&sic4<=6791)
	replace indCode48=47 if (sic4>=6792&sic4<=6792)
	replace indCode48=47 if (sic4>=6793&sic4<=6793)
	replace indCode48=47 if (sic4>=6794&sic4<=6794)
	replace indCode48=47 if (sic4>=6795&sic4<=6795)
	replace indCode48=47 if (sic4>=6798&sic4<=6798)
	replace indCode48=47 if (sic4>=6799&sic4<=6799)
	*replace indCode48=48 if (sic4>=4950&sic4<=4959)
	*replace indCode48=48 if (sic4>=4960&sic4<=4961)
	*replace indCode48=48 if (sic4>=4970&sic4<=4971)
	*replace indCode48=48 if (sic4>=4990&sic4<=4991)
	label define indCode48Names 1 "Agriculture" 2 "Food Products" 3 "Candy & Soda" 4 "Beer & Liquor" 5 "Tobacco Products" 6 "Recreation" 7 "Entertainment" 8 "Printing and Publishing" 9 "Consumer Goods" 10 "Apparel" 11 "Healthcare" 12 "Medical Equipment" 13 "Pharmaceutical Products" 14 "Chemicals" 15 "Rubber and Plastic Products" 16 "Textiles" 17 "Construction Materials" 18 "Construction" 19 "Steel Works Etc" 20 "Fabricated Products" 21 "Machinery" 22 "Electrical Equipment" 23 "Automobiles and Trucks" 24 "Aircraft" 25 "Shipbuilding, Railroad Equipment" 26 "Defense" 27 "Precious Metals" 28 "Non-Metallic and Industrial Metal Mining" 29 "Coal" 30 "Petroleum and Natural Gas" 31 "Utilities" 32 "Communication" 33 "Personal Services" 34 "Business Services" 35 "Computers" 36 "Electronic Equipment" 37 "Measuring and Control Equipment" 38 "Business Supplies" 39 "Shipping Containers" 40 "Transportation" 41 "Wholesale" 42 "Retail" 43 "Restaraunts, Hotels, Motels" 44 "Banking" 45 "Insurance" 46 "Real Estate" 47 "Trading" 48 "Almost Nothing"
	label values indCode48 indCode48Names
	
	* Identify repurchases from Amount Outstanding History File 
	joinby issue_id using amtOut_hist_agg, unm(m)
	replace action_type=action_type_hist if _merge==3
	replace effective_date=effective_date_hist if _merge==3 
	replace action_amount=action_amount_hist if _merge==3
	replace amount_outstanding=amount_outstanding_hist if _merge==3
	replace action_price=action_price_hist if _merge==3
	drop _merge
	* CRSP merge
	merge m:1 issuer_cusip yrMth using crspMonthlyLink, keepusing(permco gvkey)
	drop if _merge == 2
	drop _merge		
	rename (permco gvkey) (permco_mLink gvkey_mLink)
	 
	sort issuer_id offering_date
	replace permco_mLink = permco_mLink[_n-1] if issuer_id==issuer_id[_n-1] & permco_mLink==.
	replace gvkey_mLink = gvkey_mLink[_n-1] if issuer_id==issuer_id[_n-1] & gvkey_mLink==""
	gsort issuer_id -offering_date
	replace permco_mLink = permco_mLink[_n-1] if issuer_id==issuer_id[_n-1] & permco_mLink==.
	replace gvkey_mLink = gvkey_mLink[_n-1] if issuer_id==issuer_id[_n-1] & gvkey_mLink==""
	
	rename complete_cusip cusip
	*merge 1:m cusip using bondCRSPLink, keepusing (permco crsp_startdt crsp_enddt)
	joinby cusip using bondCRSPLink_shrt, unm(m)
	drop if _merge == 2
	drop _merge
	rename (permco crsp_startdt crsp_enddt) (permco_bLink crsp_startdt_bLink crsp_enddt_bLink)
	sort issuer_id offering_date
	replace permco_bLink = permco_bLink[_n-1] if issuer_id==issuer_id[_n-1] & permco_bLink==.
	gsort issuer_id -offering_date
	replace permco_bLink = permco_bLink[_n-1] if issuer_id==issuer_id[_n-1] & permco_bLink==.	
	replace permco_bLink=. if (offering_date<crsp_startdt_bLink | offering_date>crsp_enddt_bLink)	
	gen permco = permco_mLink
	replace permco=permco_bLink if permco==. 
	drop if permco==.
	duplicates drop issue_id permco effective_date, force
	merge m:1 permco yrMth using `gvkeyFile', keepusing(gvkey)
	drop if _merge == 2
	rename gvkey gvkey_fLink
	gen gvkey = gvkey_mLink
	replace gvkey=gvkey_fLink if gvkey==""
	drop _merge permco_mLink gvkey_mLink permco_bLink crsp_startdt_bLink crsp_enddt_bLink gvkey_fLink
	* Compustat merge
	merge m:1 gvkey yr using `compuLink', keepusing(issuer_cusip_head)
	keep if _merge == 3
	drop _merge	issuer_cusip_head
		
	******************************
	gen event=1
	egen issue=tag(issue_id)
	egen issuer=tag(issuer_id)
	egen crspEvent=sum(event)
	egen crspIssue=sum(issue)
	egen crspIssuer=sum(issuer)
	drop event issue issuer
	******************************
	
	gen repurchase=cond(action_type=="IRP" | action_type=="T",1,0)
	*keep if repurchase==1
	* Merge intial ratings data
	merge m:1 issue_id using fisdInitalRating, keepusing(ratingDPR ratingFR ratingMR ratingSPR)
	drop if _merge==2
	drop _merge
	
	* Merge ratings around change data
	merge m:1 issue_id effective_date action_type using fisdChangeRating, keepusing(ratingDPR_close1 ratingDPR_close2 ratingDPR_close3 ratingFR_close1 ratingFR_close2 ratingFR_close3 ratingMR_close1 ratingMR_close2 ratingMR_close3 ratingSPR_close1 ratingSPR_close2 ratingSPR_close3)
	drop if _merge==2
	drop _merge	
save fisdSample, replace
	keep issuer_cusip issue_cusip issue_id issuer_id action_type effective_date permco gvkey offering_date
	duplicates drop
save fisProcess, replace


****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* 5. Merge with SDC
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
clear
use sdcMnA_linked
	rename permco_acq permco
	joinby permco using fisProcess, unm(b)
	keep if _merge == 3
	gen check = dateannounced-effective_date
	gen chngB4MnA_acq=cond(check>0&check<=730,1,.)
	gen repB4MnA_acq=cond(action_type=="IRP" | action_type=="T",1,.) if chngB4MnA_acq==1
	gen checkAftr = effective_date-dateannounced
	gen chngAftMnA_acq=cond(checkAftr>0&checkAftr<=730,1,.)
	gen repAftMnA_acq=cond(action_type=="IRP" | action_type=="T",1,.) if chngAftMnA_acq==1
	*keep if repB4MnA_acq==1
	*keep if chngB4MnA_acq==1
	*keep issue_id chngB4MnA_acq effective_date 
	*duplicates drop
	collapse chngB4MnA_acq repB4MnA_acq chngAftMnA_acq repAftMnA_acq, by (issue_id effective_date)
tempfile chngB4MnA_acq
save `chngB4MnA_acq'
clear
use sdcMnA_linked
	rename permco_trgt permco
	joinby permco using fisProcess, unm(b)
	keep if _merge == 3
	gen check = dateannounced-effective_date
	gen chngB4MnA_trgt=cond(check>0&check<=730,1,.)
	gen repB4MnA_trgt=cond(action_type=="IRP" | action_type=="T",1,.) if chngB4MnA_trgt==1
	gen checkAftr = effective_date-dateannounced
	gen chngAftMnA_trgt=cond(checkAftr>0&checkAftr<=730,1,.)
	gen repAftMnA_trgt=cond(action_type=="IRP" | action_type=="T",1,.) if chngAftMnA_trgt==1
	*keep if repB4MnA_trgt==1
	*keep if chngB4MnA_trgt==1
	*keep issue_id chngB4MnA_trgt effective_date permco
	*duplicates drop
	collapse chngB4MnA_trgt repB4MnA_trgt chngAftMnA_trgt repAftMnA_trgt, by (issue_id effective_date)
tempfile chngB4MnA_trgt
save `chngB4MnA_trgt'
clear
use fisdSample 
	* SDC merge
	merge m:1 issue_id effective_date using `chngB4MnA_acq', keepusing(chngB4MnA_acq repB4MnA_acq chngAftMnA_acq repAftMnA_acq)
	drop if _merge == 2
	gen acquiror=cond(_merge==3,1,0)
	drop _merge
	merge m:1 issue_id effective_date using `chngB4MnA_trgt', keepusing(chngB4MnA_trgt repB4MnA_trgt chngAftMnA_trgt repAftMnA_trgt)
	drop if _merge == 2
	gen target=cond(_merge==3,1,0)
	drop _merge
	
	merge m:1 cusip using traceCusips	
	drop if _merge==2
	gen inTRACE=cond(_merge==3,1,0)
	drop _merge

	
	******************************
	gen event=1  if acquiror==1
	egen issue=tag(issue_id) if acquiror==1
	egen issuer=tag(issuer_id)  if acquiror==1
	egen acqEvent=sum(event)
	egen acqIssue=sum(issue)
	egen acqIssuer=sum(issuer)
	drop event issue issuer
	******************************

	******************************
	gen event=1  if target==1
	egen issue=tag(issue_id) if target==1
	egen issuer=tag(issuer_id)  if target==1
	egen tgtEvent=sum(event)
	egen tgtIssue=sum(issue)
	egen tgtIssuer=sum(issuer)
	drop event issue issuer
	******************************

	******************************
	gen event=1  if repurchase==1
	egen issue=tag(issue_id) if repurchase==1
	egen issuer=tag(issuer_id)  if repurchase==1
	egen repEvent=sum(event)
	egen repIssue=sum(issue)
	egen repIssuer=sum(issuer)
	drop event issue issuer
	******************************	
	
	******************************
	gen event=1  if inTRACE==1 & repurchase==1
	egen issue=tag(issue_id) if inTRACE==1 & repurchase==1
	egen issuer=tag(issuer_id)  if inTRACE==1 & repurchase==1
	egen trcEvent=sum(event)
	egen trcIssue=sum(issue)
	egen trcIssuer=sum(issuer)
	drop event issue issuer
	******************************
	
	******************************
	gen event=1  if repB4MnA_acq==1 & inTRACE==1
	egen issue=tag(issue_id) if repB4MnA_acq==1 & inTRACE==1
	egen issuer=tag(issuer_id)  if repB4MnA_acq==1 & inTRACE==1
	egen acqRepEvent=sum(event)
	egen acqRepIssue=sum(issue)
	egen acqRepIssuer=sum(issuer)
	drop event issue issuer
	******************************

	******************************
	gen event=1  if repB4MnA_trgt==1 & inTRACE==1
	egen issue=tag(issue_id) if repB4MnA_trgt==1 & inTRACE==1
	egen issuer=tag(issuer_id)  if repB4MnA_trgt==1 & inTRACE==1
	egen tgtRepEvent=sum(event)
	egen tgtRepIssue=sum(issue)
	egen tgtRepIssuer=sum(issuer)
	drop event issue issuer
	******************************
	
	drop fisdEvent usaEvent corpEvent optEvent aft90Event crspEvent acqEvent tgtEvent acqRepEvent tgtRepEvent repEvent trcEvent ///
		 fisdIssue usaIssue corpIssue optIssue aft90Issue crspIssue acqIssue tgtIssue acqRepIssue tgtRepIssue repIssue trcIssue ///
		 fisdIssuer usaIssuer corpIssuer optIssuer aft90Issuer crspIssuer acqIssuer tgtIssuer  acqRepIssuer tgtRepIssuer repIssuer trcIssuer
		 

	* Financials 1 qtr before Change/effective date
	gen calYrQtr=yq(year(effective_date), quarter(effective_date)) 
	format %tq calYrQtr
	replace calYrQtr=calYrQtr-1
	merge m:1 gvkey calYrQtr using compuQtrly, keepusing(chq atq oibdpq roa dlcq dlttq dtepq capxy sppey icaptq mb ret1Qtr tobinsq pe saleGrowth lev)
	drop if _merge ==2
	drop _merge
	replace calYrQtr=calYrQtr+1
	rename (chq atq oibdp roa dlcq dlttq dtepq capxy sppey icaptq mb ret1Qtr tobinsq pe saleGrowth) (chq_ef atq_ef oibdp_ef roa_ef dlcq_ef dlttq_ef dtepq_ef capxy_ef sppey_ef icaptq_ef mb_ef ret1Qtr_ef tobinsq_ef pe_ef saleGrowth_ef)
	
	* For gain/loss of debt extinguishment
	merge m:1 gvkey calYrQtr using compuQtrly, keepusing(atq dtepq)
	drop if _merge ==2
	drop _merge
	rename calYrQtr calYrQtr_ef
	
	gen cash_at=chq_ef/atq_ef
	gen debt_at=(dlcq_ef+dlttq_ef)/atq_ef
	gen cash_shrtDebt=chq_ef/dlcq_ef
	gen cash_debt=chq_ef/(dlcq_ef+dlttq_ef)
	gen dtepq_at=dtepq/atq
	
	gen repAmt_offerAmt=action_amount/offering_amt
	gen initialMaturity=(maturity-offering_date)/365
	gen remainMaturity=(maturity-effective_date)/365
	gen repAmt_at=action_amount/(atq_ef*1000000)
	*replace action_amount=action_amount/1000
	
	gen covenantDum=cond(covenants=="Y",1,0)
	gen convertibleDum=cond(convertible=="Y",1,0)
	gen callDum = cond(callable=="Y",1,0)
	gen wholeDum = cond(make_whole=="Y",1,0)
	gen straight = cond(putable!="Y" & callable!="Y" & convertible!="Y",1,0)
	gen openMrkt=cond(action_type=="IRP",1,0)
	
	la var offering_yield "Offering Yield"
	la var cash_at "(Cash/Total Assets)\$ _{t-1} \$"
	la var debt_at "(Total Debt/Total Assets)\$ _{t-1} \$"
	la var cash_shrtDebt "(Cash/Short-term Debt)\$ _{t-1} \$"
	la var cash_debt "(Cash/Total Debt)\$ _{t-1} \$"
	la var dtepq_at "Gain/Loss from Debt Reduction/Total Assets"
	la var action_amount "Repurchase Amt. (mil.)"
	la var offering_amt "Offering Amt. (mil.)"
	la var offering_yield "Offering Yield"
	la var repAmt_offerAmt "Repurchase Amt./Offer Amount"
	la var repAmt_at "Repurchase Amt./Total Assets"
	la var initialMaturity "Initial Maturity (Yrs.)"
	la var remainMaturity "Remaining Maturity (Yrs.)"
	la var covenantDum "Covenants"
	la var convertibleDum "Convertible"
	la var callDum "Callable"
	la var wholeDum "Make Whole"
	la var straight "Straight"
	la var secured "Secured"
	la var openMrkt "Open Market"	
	la var lev "Leverage\$ _{t-1} \$"
	la var atq_ef "Total Assets (mil.)\$ _{t-1} \$"
	la var tobinsq "Tobins Q\$ _{t-1} \$"
	la var pe_ef "P\/E ratio"
	la var roa_ef "Return on Assets\$ _{t-1} \$"
	la var icaptq_ef "Invested Capital\$ _{t-1} \$"
	la var mb_ef "Market-to-Book\$ _{t-1} \$"
	la var ret1Qtr_ef "Stock Return\$ _{t-1} \$"
	la var saleGrowth_ef "Sales Growth\$ _{t-1} \$"
save fisdSample_MnA_TRACE1, replace


****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Covenant reduction analysis following NFA Discussion: see NFA_meeting_Sep_2019_New stylized facts.docx for notes and discussion
*		a. Figure out old and new issues around a repurchase event
*		b. Codify convenants 	
*	
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************

clear
use fisdSample_MnA_TRACE1
	gen voting_power_cov=cond(voting_power_percentage!=.&voting_power_percentage!=0,1,0) 
	gen voting_power_erp_cov=cond(voting_power_percentage_erp!=.&voting_power_percentage_erp!=0,1,0) 
	gen declining_net_worth_t_cov=cond(declining_net_worth_trigger!=.&declining_net_worth_trigger!=0,1,0)
	gen declining_net_worth_p_cov=cond(declining_net_worth_percentage!=.&declining_net_worth_percentage!=0,1,0)
	gen declining_net_worth_pr_cov=cond(declining_net_worth_provisions!="",1,0)
	gen declining_net_worth_cov=cond(declining_net_worth_p_cov+declining_net_worth_t_cov+declining_net_worth_pr_cov>0,1,0)
	gen rating_decline_cov=cond(rating_decline_provision!="",1,0)
	
	local vars after_acquired_property_clause maintenance_net_worth consolidation_merger stock_transfer_sale_disp investments transaction_affiliates investments_unrestricted_subs fixed_charge_coverage_is subsidiary_redesignation fixed_charge_coverage_sub dividends_related_payments_is dividends_related_payments_sub restricted_payments negative_pledge_covenant subordinated_debt_issuance net_earnings_test_issuance leverage_test_is borrowing_restricted subsidiary_guarantee leverage_test_sub asset_sale_clause sales_leaseback_is sale_assets sale_xfer_assets_unrestricted sales_leaseback_sub funded_debt_is indebtedness_is liens_is senior_debt_issuance funded_debt_sub indebtedness_sub liens_sub stock_issuance_issuer stock_issuance preferred_stock_issuance cross_default cross_acceleration change_control_put_provisions rating_decline_trigger_put 
	foreach v of local vars{
		replace `v'=upper(trim(`v'))
		replace `v'="0" if `v'=="N" | `v'==""   
		replace `v'="1" if `v'=="Y" 
		destring `v', replace
	}
	
	* Sub-type variable definitions
	gen indirect_Inv_Res=cond(after_acquired_property_clause+maintenance_net_worth>0,1,0)
	gen merger_res=cond(consolidation_merger>0,1,0)
	gen stock_sale_res=cond(stock_transfer_sale_disp>0,1,0)
	gen direct_inv_res=cond(investments+transaction_affiliates+investments_unrestricted_subs>0,1,0)
	gen inv_res=cond(fixed_charge_coverage_is+subsidiary_redesignation+fixed_charge_coverage_sub>0,1,0)
	
	gen div_pay_res=cond(dividends_related_payments_is+dividends_related_payments_sub>0,1,0)
	gen other_pay_res=cond(restricted_payments>0,1,0)
	*gen divOther_pay_res 

	gen subord_deb_issue_res=cond(negative_pledge_covenant+subordinated_debt_issuance+net_earnings_test_issuance+leverage_test_is+borrowing_restricted+subsidiary_guarantee+leverage_test_sub>0,1,0)
	gen saleLease_ob_res=cond(asset_sale_clause+sales_leaseback_is+sale_assets+sale_xfer_assets_unrestricted+sales_leaseback_sub>0,1,0)
	gen debt_priority_res=cond(funded_debt_is+indebtedness_is+liens_is+senior_debt_issuance+funded_debt_sub+indebtedness_sub+liens_sub>0,1,0)
	gen stock_iss_res=cond(stock_issuance_issuer+stock_issuance+preferred_stock_issuance>0,1,0)
	*gen sub_fin_res
	
	gen default_res=cond(cross_default+cross_acceleration>0,1,0)
	gen control_change_res=cond(change_control_put_provisions+voting_power_cov+voting_power_erp_cov>0,1,0)
	gen event_res=cond(rating_decline_trigger_put+rating_decline_cov+declining_net_worth_cov>0,1,0)
	
	* Umberalla type variable definitions
	gen inv_res_umb=cond(indirect_Inv_Res+merger_res+stock_sale_res+direct_inv_res+inv_res+secured>0,1,0)
	gen div_res_umb=cond(div_pay_res+other_pay_res>0,1,0) 
	gen sub_fin_res_umb=cond(subord_deb_issue_res+saleLease_ob_res+debt_priority_res+stock_iss_res>0,1,0)
	gen event_res_umb=cond(default_res+control_change_res+event_res>0,1,0)
	
	la var indirect_Inv_Res "\hspace{0.3in} Indirect Investment"
	la var merger_res "\hspace{0.3in} Merger"
	la var stock_sale_res "\hspace{0.3in} Stock Sale"
	la var direct_inv_res "\hspace{0.3in} Direct Investment"
	la var inv_res "\hspace{0.3in} Investment"

	la var div_pay_res "\hspace{0.3in} Dividend Payment"
	la var other_pay_res "\hspace{0.3in} Other Payment"

	la var subord_deb_issue_res "\hspace{0.3in} Subordinate Debt Issuance"
	la var saleLease_ob_res "\hspace{0.3in} Sale/Lease Obligation"
	la var debt_priority_res "\hspace{0.3in} Debt Priority"
	la var stock_iss_res "\hspace{0.3in} Stock Issuance"

	la var default_res "\hspace{0.3in} Default event"
	la var control_change_res "\hspace{0.3in} Change in Control"
	la var event_res "\hspace{0.3in} Events"

	la var inv_res_umb "\textbf{Investment-related Restrictions}"
	la var div_res_umb "\textbf{Divident-related Restrictions}"
	la var sub_fin_res_umb "\textbf{Subsequent Financing-related Restrictions}"
	la var event_res_umb "\textbf{Event-related Restrictions}"	
save fisdSample_MnA_TRACE_cov, replace	
clear
use fisdSample_MnA_TRACE_cov
    gen other_Inv_res = cond(indirect_Inv_Res+stock_sale_res+inv_res+secured>0,1,0)
	gen other_event_res=cond(default_res+event_res>0,1,0)
    la var inv_res_umb "{Investment-related Restrictions}"
    la var merger_res "\hspace{0.3in} Merger"
	la var direct_inv_res "\hspace{0.3in} Direct Investment"
    la var other_Inv_res "\hspace{0.3in} Other Invesmtent"
    la var div_res_umb "{Dividend-related Restrictions}"
	la var sub_fin_res_umb "{Subsequent Financing-related Restrictions}"
	la var event_res_umb "{Event-related Restrictions}"	
	la var control_change_res "\hspace{0.3in} Change in Control"
	la var other_event_res "\hspace{0.3in} Other Event"
save fisdSample_MnA_TRACE_cov_grpd, replace

****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Build a firm-year panel with bond, covenants, repurchases, M&A activity, & other market measures
* Get M&A Event dates 2 years before each repurchase event
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************	

* New bonds issued by quarter & their covenants
clear
use fisdSample_MnA_TRACE_cov	
	keep issuer_id issue_id permco gvkey offering_date offering_amt offering_yield treasury_spread initialMaturity  ///
			inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
			div_res_umb div_pay_res other_pay_res ///
			sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
			event_res_umb default_res control_change_res event_res callDum wholeDum convertibleDum
	duplicates drop		
	gen noIssue=1		
	gen offerYrQtr=yq(year(offering_date), quarter(offering_date)) 
	format %tq offerYrQtr
	collapse (sum) offering_amt inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
					div_res_umb div_pay_res other_pay_res ///
					sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
					event_res_umb default_res control_change_res event_res noIssue ///
					callDum wholeDum convertibleDum ///
			(mean)  offering_yield treasury_spread initialMaturity , by(issuer_id permco gvkey offerYrQtr)
	
	local vars offering_amt inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
					div_res_umb div_pay_res other_pay_res ///
					sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
					event_res_umb default_res control_change_res event_res noIssue ///
					callDum wholeDum convertibleDum ///
					offering_yield treasury_spread initialMaturity 	
	foreach v of local vars{
		rename `v' nb_`v'
	}
	
tempfile newBondsByQtr	
save `newBondsByQtr'

* Repurchased bonds by quarter & their covenants
clear
use fisdSample_MnA_TRACE_cov	
	keep if repurchase==1
	gen repCalYrQtr=yq(year(effective_date), quarter(effective_date)) 
	format %tq repCalYrQtr
	collapse (sum) inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
					div_res_umb div_pay_res other_pay_res ///
					sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
					callDum wholeDum convertibleDum ///
					event_res_umb default_res control_change_res event_res noIssue=repurchase, by(issuer_id permco gvkey repCalYrQtr)
	
	local vars inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				div_res_umb div_pay_res other_pay_res ///
				sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				callDum wholeDum convertibleDum ///
				event_res_umb default_res control_change_res event_res noIssue
	foreach v of local vars{
		rename `v' rb_`v'
	}
	
tempfile repBondsByQtr	
save `repBondsByQtr'

	
*Repurchase events
clear
use issue_id issuer_id permco gvkey  effective_date repurchase openMrkt using fisdSample_MnA_TRACE_cov	
	gen calYrQtr=yq(year(effective_date), quarter(effective_date)) 
	format %tq calYrQtr
	gen tenderOffer=(1-openMrkt) if repurchase==1
	replace tenderOffer=0 if tenderOffer==.
	su repurchase openMrkt tenderOffer if repurchase==1
	keep issue_id issuer_id permco gvkey calYrQtr repurchase openMrkt tenderOffer 
	duplicates drop
	collapse (sum) repurchase openMrkt tenderOffer , by(issue_id issuer_id permco gvkey   calYrQtr)
tempfile repurchaseEvents	
save `repurchaseEvents'
	
*Covenants
clear
use fisdSample_MnA_TRACE_cov	
	keep issuer_id issue_id  permco gvkey inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				div_res_umb div_pay_res other_pay_res ///
				sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				event_res_umb default_res control_change_res event_res	///
				callDum wholeDum convertibleDum 
	duplicates drop 
tempfile covenants	
save `covenants'
* Tracking changes to bond issues over time
clear
use issue_id using fisdSample_MnA_TRACE_cov
	duplicates drop 
tempfile issueIDs	
save `issueIDs'
clear
use amtOut	
	append using amtOut_hist
	sort issue_id effective_date action_type 
	merge m:1 issue_id using `issueIDs'
	keep if _merge ==3
	drop _merge
tempfile issueEvents	
save `issueEvents'

clear
use issue_id offering_amt offering_date using fisdSample_MnA_TRACE_cov
	gen action_type="I"
	gen effective_date=offering_date
	gen amount_outstanding=offering_amt
	append using `issueEvents'
	sort issue_id effective_date
	duplicates drop issue_id effective_date action_type amount_outstanding, force
	drop offering_amt offering_date 
tempfile issueAllEvents	
save `issueAllEvents'
clear
use issue_id issuer_id offering_amt offering_date delivery_date maturity using fisdSample_MnA_TRACE_cov	
	duplicates drop issue_id issuer_id offering_amt offering_date delivery_date maturity, force
	merge 1:m issue_id using `issueAllEvents', keepusing(action_type effective_date amount_outstanding action_price action_amount transaction_id)
	drop if _merge==2
	drop _merge
	sort issuer_id issue_id effective_date
	format effective_date %td
	gen calYrQtr=yq(year(effective_date),quarter(effective_date))
	format calYrQtr %tq
	sort issue_id issuer_id calYrQtr effective_date
	bysort issue_id issuer_id calYrQtr: gen keeper=cond(_n==_N,1,0)
	keep if keeper==1
tempfile issueDetails
save `issueDetails'	
	
* M&A tracking over quarters for 
clear
use sdcMnA_linked
	gen calYrQtr=yq(year(dateannounced), quarter(dateannounced))
	format %tq calYrQtr
	keep permco_acq calYrQtr
	drop if permco_acq==.
	gen noAcquisitions=1
	collapse (sum) noAcquisitions, by(permco_acq calYrQtr)
	rename permco_acq permco
tempfile acqQtrs
save `acqQtrs'
clear
use sdcMnA_linked
	gen calYrQtr=yq(year(dateannounced), quarter(dateannounced))
	format %tq calYrQtr
	keep permco_trgt calYrQtr
	drop if permco_trgt==.
	gen noTargets=1
	collapse (sum) noTargets, by(permco_trgt calYrQtr)
	rename permco_trgt permco
tempfile trgQtrs
save `trgQtrs'	

* Year-Quarters for panel creation
clear all
set obs 136
	gen calYrQtr=.
	forvalues i=1(1)136{
		replace calYrQtr=`i'+99 if _n==`i'
	}
	format calYrQtr %tq 
	gen id=1
tempfile quarters	
save `quarters'
* Panel Creation
clear
use issuer_id issue_id permco gvkey offering_date  offering_amt offering_yield treasury_spread initialMaturity maturity using fisdSample_MnA_TRACE_cov
	duplicates drop
	gen id=1
	joinby id using `quarters'
	drop id
	sort issuer_id issue_id calYrQtr 
	drop if calYrQtr<(yq(year(offering_date),quarter(offering_date)))
	gen remainMaturity=(yq(year(maturity),quarter(maturity))-calYrQtr)/4
	drop if remainMaturity<0
	merge m:1 issuer_id issue_id calYrQtr using `issueDetails', keepusing(amount_outstanding action_type effective_date)
	drop if _merge==2
	drop _merge
	sort issuer_id issue_id  permco gvkey calYrQtr
	gen amount_outstanding1=amount_outstanding 
	by issuer_id issue_id permco gvkey: replace amount_outstanding1=amount_outstanding1[_n-1] if amount_outstanding1==.
	sort issuer_id issue_id  permco gvkey calYrQtr 
	drop amount_outstanding action_type effective_date
	drop if amount_outstanding1==.
	rename amount_outstanding1 amount_outstanding 
	
	merge m:1 issuer_id issue_id  permco gvkey  calYrQtr using `repurchaseEvents', keepusing(repurchase openMrkt tenderOffer)
	drop if _merge==2
	drop _merge
	local vars repurchase openMrkt tenderOffer
	foreach v of local vars{
		replace `v'=0 if `v'==.
	}
	
	merge m:1 issuer_id issue_id  permco gvkey using `covenants', keepusing(inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				div_res_umb div_pay_res other_pay_res ///
				sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				event_res_umb default_res control_change_res event_res ///
				callDum wholeDum convertibleDum ) 
	drop if _merge==2
	drop _merge
	sort issuer_id permco gvkey calYrQtr issue_id 
	
	gen noIssuesOut=1 
save frmYrQtrPanel_issueSample, replace	
	collapse (sum) offering_amt amount_outstanding repurchase openMrkt tenderOffer ///
				   inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				   div_res_umb div_pay_res other_pay_res ///
				   sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				   event_res_umb default_res control_change_res event_res noIssuesOut ///
				   callDum wholeDum convertibleDum ///
			(mean) offering_yield treasury_spread initialMaturity remainMaturity, by(issuer_id permco gvkey calYrQtr)
				   
				   
	local vars repurchase openMrkt tenderOffer inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				   div_res_umb div_pay_res other_pay_res ///
				   sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				   event_res_umb default_res control_change_res event_res ///
				   callDum wholeDum convertibleDum 
				   
	foreach v of local vars{
		gen `v'_frac= `v'/noIssuesOut
	}
	
	merge m:1 gvkey calYrQtr using compuQtrly, keepusing(chq atq oibdpq roa dlcq dlttq dtepq capxy sppey icaptq mb ret1Qtr tobinsq pe saleGrowth lev atq dtepq)	
	drop if _merge==2
	gen missCompu=1
	replace missCompu=0 if _merge==3
	drop _merge
	merge m:1 permco calYrQtr using `acqQtrs'
	drop if _merge==2
	drop _merge
	merge m:1 permco calYrQtr using `trgQtrs'
	drop if _merge==2
	drop _merge
	
	local vars noAcquisitions noTargets
	foreach v of local vars{
		replace `v'=0 if `v'==.
	}
	
	
	* Select sample for detailed analysis: 
	*	(a) Compaines that did a repurchase during their life in sample
	* 	(b) 2 years around each repurcase event find: acquirors, targets, covenant reduction, new bond issues, firm characteristics
		
	bysort permco: egen repurchaseComp=sum(repurchase)
	replace repurchaseComp=cond(repurchaseComp>0,1,0)
save frmYrQtrPanel, replace
	
	clear all
	set obs 18
	gen timeLine=.
	forvalues i=0(1)18{
		replace timeLine=-10+`i' if _n==`i'
	}
	gen id=1
tempfile timeLine	
save `timeLine'
clear
use frmYrQtrPanel	
	keep if repurchaseComp==1
	keep if repurchase!=0
	keep issuer_id permco gvkey calYrQtr repurchase openMrkt tenderOffer
	rename (repurchase openMrkt tenderOffer) (repurchase_rep openMrkt_rep tenderOffer_rep)
	gen id=1
	joinby id using `timeLine'
	rename calYrQtr repYrQtr
	gen calYrQtr=repYrQtr+timeLine
	format calYrQtr %tq
	drop id  
	sort issuer_id permco gvkey repYrQtr calYrQtr
	egen repEventID=group(issuer_id permco gvkey repYrQtr)
	merge m:1 issuer_id permco gvkey calYrQtr using frmYrQtrPanel
	keep if _merge == 3
	drop _merge
	rename calYrQtr offerYrQtr
	merge m:1 issuer_id permco gvkey offerYrQtr using `newBondsByQtr', keepusing(nb_*)
	drop if _merge==2
	drop _merge
	rename offerYrQtr repCalYrQtr
	merge m:1 issuer_id permco gvkey repCalYrQtr using `repBondsByQtr', keepusing(rb_*)
	drop if _merge==2
	drop _merge
	rename repCalYrQtr calYrQtr
	sort repEventID issuer_id permco gvkey calYrQtr
	gen acq_dum=cond(noAcquisitions>0&noAcquisitions!=.,1,0)
	gen trg_dum=cond(noTargets>0&noTargets!=.,1,0)
	
	* Generate pre/post dummy variables
	local vars acq_dum trg_dum  inv_res_umb_frac div_res_umb_frac sub_fin_res_umb_frac event_res_umb_frac merger_res_frac amount_outstanding noIssuesOut
	foreach v of local vars{
		gen pre_`v'=`v' if timeLine<0
		gen post_`v'=`v' if timeLine>=0
	}
	
	* Create fractions for summary tables
	local vars nb_inv_res_umb nb_indirect_Inv_Res nb_merger_res nb_stock_sale_res nb_direct_inv_res nb_inv_res nb_secured nb_div_res_umb nb_div_pay_res nb_other_pay_res nb_sub_fin_res_umb nb_subord_deb_issue_res ///
			nb_saleLease_ob_res nb_debt_priority_res nb_stock_iss_res nb_event_res_umb nb_default_res nb_control_change_res nb_event_res  nb_callDum nb_wholeDum nb_convertibleDum 
	foreach v of local vars{
		gen `v'_frac=`v'/nb_noIssue
	}
	local vars rb_inv_res_umb rb_indirect_Inv_Res rb_merger_res rb_stock_sale_res rb_direct_inv_res rb_inv_res rb_secured rb_div_res_umb rb_div_pay_res rb_other_pay_res rb_sub_fin_res_umb rb_subord_deb_issue_res ///
			rb_saleLease_ob_res rb_debt_priority_res rb_stock_iss_res rb_event_res_umb rb_default_res rb_control_change_res rb_event_res rb_callDum rb_wholeDum rb_convertibleDum 
	foreach v of local vars{
		gen `v'_frac=`v'/rb_noIssue
	}		
	
	local vars repurchase openMrkt tenderOffer
	foreach v of local vars{
		replace `v'=. if timeLine!=0
	}
	
	la var repurchase "\# Issues repurchased" 
	la var openMrkt "\# Open market"
	la var tenderOffer "\# Tender offers"
	la var rb_noIssue "\# Issues repurchased"
	la var rb_merger_res_frac "Merger"
	la var rb_inv_res_umb_frac "Investment-related"
	la var rb_div_res_umb_frac "Dividend-related"
	la var rb_sub_fin_res_umb_frac "Subsequent Financing-related"
	la var rb_event_res_umb_frac "Event-related"
	la var nb_noIssue "\# New Issues"
	la var nb_merger_res_frac "Merger"
	la var nb_inv_res_umb_frac "Investment-related"
	la var nb_div_res_umb_frac "Dividend-related"
	la var nb_sub_fin_res_umb_frac "Subsequent Financing-related"
	la var nb_event_res_umb_frac "Event-related"
	la var pre_acq_dum "Acquirer before repurchase "
	la var post_acq_dum "Acquirer after repurchase "
	la var pre_trg_dum "Target before repurchase"
	la var post_trg_dum "Target after repurchase "
	la var noIssuesOut "\# Issues outstanding"
	la var pre_noIssue "\# Issues repurchased"
	la var pre_amount_outstanding "Amount Outstanding"
	la var pre_merger_res_frac "Merger"
	la var pre_inv_res_umb_frac "Investment-related"
	la var pre_div_res_umb_frac "Dividend-related"
	la var pre_sub_fin_res_umb_frac "Subsequent Financing-related"
	la var pre_event_res_umb_frac "Event-related"
	la var post_noIssue "\# Issues repurchased"
	la var post_amount_outstanding "Amount Outstanding"
	la var post_merger_res_frac "Merger"
	la var post_inv_res_umb_frac "Investment-related"
	la var post_div_res_umb_frac "Dividend-related"
	la var post_sub_fin_res_umb_frac "Subsequent Financing-related"
	la var post_event_res_umb_frac "Event-related"
	la var rb_callDum_frac "Callable"
	la var rb_wholeDum_frac "Make Whole"
	la var rb_convertibleDum_frac "Convertible"
	la var nb_callDum_frac "Callable"
	la var nb_wholeDum_frac "Make Whole"
	la var nb_convertibleDum_frac "Convertible"
	replace pre_amount_outstanding=pre_amount_outstanding/1000000	
	replace post_amount_outstanding=post_amount_outstanding/1000000	
	sort issuer_id	permco	gvkey	repYrQtr timeLine
save frmYrQtrPanel_repSample, replace	


				
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* Cross-sectional analysis instead of panel-based analysis 
****************************************************************************************************************************************************************

* Bonds outstanding as of 1-quarter prior to repurchasing event quarter
clear
use frmYrQtrPanel_repSample
	keep if timeLine==-1
	duplicates drop issuer_id permco gvkey repYrQtr calYrQtr repEventID, force
	drop calYrQtr 
	rename repYrQtr calYrQtr_ef 
	
	keep issuer_id permco gvkey calYrQtr_ef  amount_outstanding offering_yield treasury_spread initialMaturity remainMaturity noIssuesOut ///
				   inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				   div_res_umb div_pay_res other_pay_res ///
				   sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				   event_res_umb default_res control_change_res event_res ///
				   inv_res_umb_frac indirect_Inv_Res_frac merger_res_frac stock_sale_res_frac direct_inv_res_frac inv_res_frac secured_frac ///
				   div_res_umb_frac div_pay_res_frac other_pay_res_frac ///
				   sub_fin_res_umb_frac subord_deb_issue_res_frac saleLease_ob_res_frac debt_priority_res_frac stock_iss_res_frac ///
				   event_res_umb_frac default_res_frac control_change_res_frac event_res_frac ///
				   callDum wholeDum convertibleDum callDum_frac wholeDum_frac convertibleDum_frac
				   
	local vars amount_outstanding offering_yield treasury_spread initialMaturity remainMaturity noIssuesOut ///
				   inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				   div_res_umb div_pay_res other_pay_res ///
				   sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				   event_res_umb default_res control_change_res event_res ///
				   inv_res_umb_frac indirect_Inv_Res_frac merger_res_frac stock_sale_res_frac direct_inv_res_frac inv_res_frac secured_frac ///
				   div_res_umb_frac div_pay_res_frac other_pay_res_frac ///
				   sub_fin_res_umb_frac subord_deb_issue_res_frac saleLease_ob_res_frac debt_priority_res_frac stock_iss_res_frac ///
				   event_res_umb_frac default_res_frac control_change_res_frac event_res_frac ///
				   callDum wholeDum convertibleDum callDum_frac wholeDum_frac convertibleDum_frac
	foreach v of local vars{
		rename `v' `v'_preQ
	}
tempfile preRepQtr	
save `preRepQtr'

* Post repurchase bonds outstanding
clear
use frmYrQtrPanel_repSample
	keep if timeLine>=0
	duplicates drop issuer_id permco gvkey repYrQtr calYrQtr repEventID, force
	drop calYrQtr 
	rename repYrQtr calYrQtr_ef 
	sort issuer_id permco gvkey calYrQtr_ef
	
	collapse (mean) amount_outstanding offering_yield treasury_spread initialMaturity remainMaturity noIssuesOut ///
				   inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				   div_res_umb div_pay_res other_pay_res ///
				   sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				   event_res_umb default_res control_change_res event_res ///
				   inv_res_umb_frac indirect_Inv_Res_frac merger_res_frac stock_sale_res_frac direct_inv_res_frac inv_res_frac secured_frac ///
				   div_res_umb_frac div_pay_res_frac other_pay_res_frac ///
				   sub_fin_res_umb_frac subord_deb_issue_res_frac saleLease_ob_res_frac debt_priority_res_frac stock_iss_res_frac ///
				   event_res_umb_frac default_res_frac control_change_res_frac event_res_frac ///	
				   callDum wholeDum convertibleDum callDum_frac wholeDum_frac convertibleDum_frac ///
					, by(issuer_id permco gvkey calYrQtr_ef)
	   
	local vars amount_outstanding offering_yield treasury_spread initialMaturity remainMaturity noIssuesOut ///
				   inv_res_umb indirect_Inv_Res merger_res stock_sale_res direct_inv_res inv_res secured ///
				   div_res_umb div_pay_res other_pay_res ///
				   sub_fin_res_umb subord_deb_issue_res saleLease_ob_res debt_priority_res stock_iss_res ///
				   event_res_umb default_res control_change_res event_res ///
				   inv_res_umb_frac indirect_Inv_Res_frac merger_res_frac stock_sale_res_frac direct_inv_res_frac inv_res_frac secured_frac ///
				   div_res_umb_frac div_pay_res_frac other_pay_res_frac ///
				   sub_fin_res_umb_frac subord_deb_issue_res_frac saleLease_ob_res_frac debt_priority_res_frac stock_iss_res_frac ///
				   event_res_umb_frac default_res_frac control_change_res_frac event_res_frac ///
				   callDum wholeDum convertibleDum callDum_frac wholeDum_frac convertibleDum_frac
				   
	foreach v of local vars{
		rename `v' `v'_p2yr
	}
tempfile post2yr	
save `post2yr'



* New bonds issued within two years from a repurchasing event quarter
clear
use frmYrQtrPanel_repSample
	keep if timeLine>=0
	rename repYrQtr calYrQtr_ef 
	collapse (sum) nb_noIssue nb_offering_amt ///
					nb_inv_res_umb nb_indirect_Inv_Res nb_merger_res nb_stock_sale_res nb_direct_inv_res nb_inv_res nb_secured ///
					nb_div_res_umb nb_div_pay_res nb_other_pay_res ///
					nb_sub_fin_res_umb nb_subord_deb_issue_res nb_saleLease_ob_res nb_debt_priority_res nb_stock_iss_res ///
					nb_event_res_umb nb_default_res nb_control_change_res nb_event_res ///
					nb_callDum nb_wholeDum nb_convertibleDum ///
			(mean) 	nb_offering_yield nb_treasury_spread nb_initialMaturity ///
					, by(issuer_id permco gvkey calYrQtr_ef)
	
	* Create fractions for summary tables
	local vars nb_inv_res_umb nb_indirect_Inv_Res nb_merger_res nb_stock_sale_res nb_direct_inv_res nb_inv_res nb_secured ///
					nb_div_res_umb nb_div_pay_res nb_other_pay_res ///
					nb_sub_fin_res_umb nb_subord_deb_issue_res nb_saleLease_ob_res nb_debt_priority_res nb_stock_iss_res ///
					nb_event_res_umb nb_default_res nb_control_change_res nb_event_res  nb_callDum nb_wholeDum nb_convertibleDum
	foreach v of local vars{
		gen `v'_frac=`v'/nb_noIssue
	}
tempfile newIssues2yrs	
save `newIssues2yrs'

clear
use fisdSample_MnA_TRACE_cov
	keep if repurchase==1
	merge m:1 issuer_id permco gvkey calYrQtr_ef using `preRepQtr'
	drop if _merge==2
	drop _merge
	merge m:1 issuer_id permco gvkey calYrQtr_ef using `post2yr'
	drop if _merge==2
	drop _merge	
	merge m:1 issuer_id permco gvkey calYrQtr_ef using `newIssues2yrs'
	drop if _merge==2
	drop _merge

	la var noIssuesOut_preQ "\# Issues Outstanding"
	la var amount_outstanding_preQ "Total Amt. Outstanding (mil.)"
	la var offering_yield_preQ "Avg. Offering Yield"
	la var treasury_spread_preQ "Avg. Treasury Spread"
	la var initialMaturity_preQ "Avg. Initial Maturity (Yrs.)"
	la var remainMaturity_preQ "Avg. Remaining Maturity (Yrs.)"
	
	la var merger_res_frac_preQ "Merger"
	la var inv_res_umb_frac_preQ "Investment-related"
	la var div_res_umb_frac_preQ "Dividend-related"
	la var sub_fin_res_umb_frac_preQ "Subsequent Financing-related"
	la var event_res_umb_frac_preQ "Event-related"
	
	la var callDum_frac_preQ "Callable"
	la var wholeDum_frac_preQ "Make Whole"
	la var convertibleDum_frac_preQ "Convertible"
	
	la var noIssuesOut_p2yr "\# Issues Outstanding"
	la var amount_outstanding_p2yr "Total Amt. Outstanding (mil.)"
	la var offering_yield_p2yr "Avg. Offering Yield"
	la var treasury_spread_p2yr "Avg. Treasury Spread"
	la var initialMaturity_p2yr "Avg. Initial Maturity (Yrs.)"
	la var remainMaturity_p2yr "Avg. Remaining Maturity (Yrs.)"
	
	la var merger_res_frac_p2yr "Merger"
	la var inv_res_umb_frac_p2yr "Investment-related"
	la var div_res_umb_frac_p2yr "Dividend-related"
	la var sub_fin_res_umb_frac_p2yr "Subsequent Financing-related"
	la var event_res_umb_frac_p2yr "Event-related"
	
	la var callDum_frac_p2yr "Callable"
	la var wholeDum_frac_p2yr "Make Whole"
	la var convertibleDum_frac_p2yr "Convertible"
	
	la var nb_noIssue "\# New Issues"
	la var nb_offering_amt "Total Offering Amt. (mil.)"
	la var nb_offering_yield "Avg. Offering Yield"
	la var nb_treasury_spread "Avg. Treasury Spread"
	la var nb_initialMaturity "Avg. Initial Maturity (Yrs.)"	
	
	la var nb_merger_res_frac "Merger"
	la var nb_inv_res_umb_frac "Investment-related"
	la var nb_div_res_umb_frac "Dividend-related"
	la var nb_sub_fin_res_umb_frac "Subsequent Financing-related"
	la var nb_event_res_umb_frac "Event-related"

	la var nb_callDum_frac "Callable"
	la var nb_wholeDum_frac "Make Whole"
	la var nb_convertibleDum_frac "Convertible"

	
	la var repB4MnA_trgt "Target within 2yrs. post Repurchase"
	la var repB4MnA_acq "Acquirer within 2yrs. post Repurchase"
	la var repAftMnA_acq "Acquirer within 2yrs. pre Repurchase" 
	la var repAftMnA_trgt "Target within 2yrs. pre Repurchase" 

	local vars repB4MnA_acq repB4MnA_trgt repAftMnA_acq repAftMnA_trgt
	foreach v of local vars{
		replace `v'=0 if `v'==.
	}
	
	replace amount_outstanding_preQ=amount_outstanding_preQ/1000000
	replace nb_offering_amt=nb_offering_amt/1000000
	replace amount_outstanding_p2yr=amount_outstanding_p2yr/1000000
save repCS_b4Aftr, replace

* Add financial variables (1) Post 2 years average & (2) Values at the end of two years.
clear
use frmYrQtrPanel_repSample	
	keep if timeLine>0
	gen cash_at=chq/atq
	gen debt_at=(dlcq+dlttq)/atq
	gen cash_shrtDebt=chq/dlcq
	gen cash_debt=chq/(dlcq+dlttq)
	gen dtepq_at=dtepq/atq
	rename (atq mb roa icaptq tobinsq ret1Qtr) (atq_ef mb_ef roa_ef icaptq_ef tobinsq_ef ret1Qtr_ef )
	collapse (mean) atq_ef roa_ef cash_at debt_at cash_shrtDebt cash_debt lev tobinsq_ef mb_ef ret1Qtr_ef icaptq_ef dtepq_at , by(repYrQtr issuer_id gvkey permco)
	la var atq_ef "Total Assets (mil.)\$ _{t-1} \$"
	la var roa_ef "Return on Assets\$ _{t-1} \$"
	la var tobinsq "Tobins Q\$ _{t-1} \$"
	la var icaptq_ef "Invested Capital\$ _{t-1} \$"
	la var mb_ef "Market-to-Book\$ _{t-1} \$"
	la var ret1Qtr_ef "Stock Return\$ _{t-1} \$"
	la var lev "Leverage\$ _{t-1} \$"
	la var cash_at "(Cash/Total Assets)\$ _{t-1} \$"
	la var debt_at "(Total Debt/Total Assets)\$ _{t-1} \$"
	la var cash_shrtDebt "(Cash/Short-term Debt)\$ _{t-1} \$"
	la var cash_debt "(Cash/Total Debt)\$ _{t-1} \$"
	la var dtepq_at "Gain/Loss from Debt Reduction/Total Assets"
	
	local vars atq_ef roa_ef cash_at debt_at cash_shrtDebt cash_debt lev tobinsq_ef mb_ef ret1Qtr_ef icaptq_ef dtepq_at
	foreach v of local vars{
		rename `v' `v'_p2yr
	}
	rename repYrQtr calYrQtr_ef
tempfile fin_post2YrAvg
save `fin_post2YrAvg'
	
clear
use frmYrQtrPanel_repSample	
	keep if timeLine>0
	sort issuer_id gvkey permco repYrQtr timeLine
	bysort issuer_id gvkey permco repYrQtr: keep if _n==_N
	
	gen cash_at=chq/atq
	gen debt_at=(dlcq+dlttq)/atq
	gen cash_shrtDebt=chq/dlcq
	gen cash_debt=chq/(dlcq+dlttq)
	gen dtepq_at=dtepq/atq
	rename (atq mb roa icaptq tobinsq ret1Qtr) (atq_ef mb_ef roa_ef icaptq_ef tobinsq_ef ret1Qtr_ef )
	la var atq_ef "Total Assets (mil.)\$ _{t-1} \$"
	la var roa_ef "Return on Assets\$ _{t-1} \$"
	la var tobinsq "Tobins Q\$ _{t-1} \$"
	la var icaptq_ef "Invested Capital\$ _{t-1} \$"
	la var mb_ef "Market-to-Book\$ _{t-1} \$"
	la var ret1Qtr_ef "Stock Return\$ _{t-1} \$"
	la var lev "Leverage\$ _{t-1} \$"
	la var cash_at "(Cash/Total Assets)\$ _{t-1} \$"
	la var debt_at "(Total Debt/Total Assets)\$ _{t-1} \$"
	la var cash_shrtDebt "(Cash/Short-term Debt)\$ _{t-1} \$"
	la var cash_debt "(Cash/Total Debt)\$ _{t-1} \$"
	la var dtepq_at "Gain/Loss from Debt Reduction/Total Assets"
	keep atq_ef roa cash_at debt_at cash_shrtDebt cash_debt lev tobinsq mb ret1Qtr icaptq dtepq_at issuer_id gvkey permco repYrQtr 
	local vars atq_ef roa_ef cash_at debt_at cash_shrtDebt cash_debt lev tobinsq_ef mb_ef ret1Qtr_ef icaptq_ef dtepq_at
	foreach v of local vars{
		rename `v' nb_`v'
	}
	rename repYrQtr calYrQtr_ef
tempfile fin_post2Yr
save `fin_post2Yr'

	
	
clear
use repCS_b4Aftr	
	merge m:1 issuer_id gvkey permco calYrQtr_ef using `fin_post2YrAvg'
	drop if _merge==2
	drop _merge
	merge m:1 issuer_id gvkey permco calYrQtr_ef using `fin_post2Yr'
	drop if _merge==2
	drop _merge
	replace mb_ef=. if mb_ef<0
	replace mb_ef_p2yr=. if mb_ef_p2yr<0
	replace nb_mb_ef=. if nb_mb_ef<0
save repCS_b4Aftr_wFin, replace	
	
******************************************************************************************************************************************
*SAMPLE MATCHING WITHIN: (1) Industry (2) Repurchase Year (3) Repurchase Type (4) Assets (5) Leverage 	
******************************************************************************************************************************************

clear
use gvkey atq lev cash_at tobinsq ret1Qtr calYrQtr using compuQtrly
tempfile compuData
save `compuData'

clear
use repCS_b4Aftr_wFin
	tab repB4MnA_acq
	gen fyear = year(effective_date)
	bysort issuer_id gvkey calYrQtr_ef: egen acq=sum(repB4MnA_acq)
	replace acq=1 if acq>0
	sort  gvkey calYrQtr_ef
	tab acq
	keep issuer_id gvkey calYrQtr_ef acq indCode fyear openMrkt 
	duplicates drop
	joinby gvkey using `compuData'
	
	gen treatQtr = cond(calYrQtr==calYrQtr_ef-4| calYrQtr==calYrQtr_ef-3 | calYrQtr==calYrQtr_ef-2 | calYrQtr==calYrQtr_ef-1 | calYrQtr==calYrQtr_ef ,1,0) 
	drop if treatQtr==0
	tab acq
	gsort -acq gvkey calYrQtr_ef calYrQtr 
	drop treatQtr 
	gen lnAtq=ln(1+atq) 
	sort issuer_id gvkey calYrQtr_ef calYrQtr
	

	capture teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (acq), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) osample(check) nn(1)
	tab check
	tab check if acq==1
	drop if check==1
	drop check

	capture teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (acq), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) osample(check) nn(1)
	tab check
	tab check if acq==1
	drop if check==1
	drop check
	
	capture teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (acq), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) osample(check) nn(1)
	tab check
	tab check if acq==1
	drop if check==1
	drop check	

	gsort -acq issuer_id gvkey calYrQtr_ef calYrQtr 
	gen rNum=_n	
	teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (acq), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) gen(matches) nn(1)		
	
	reshape long matches, i(issuer_id gvkey calYrQtr_ef openMrkt  calYrQtr) j(match) 
	sort rNum issuer_id gvkey calYrQtr_ef openMrkt calYrQtr match
	drop if matches==.
	egen repQtrID=group(issuer_id gvkey calYrQtr_ef openMrkt)
save fullAcqMatches_repMth, replace


clear
use fullAcqMatches_repMth
	keep if acq==1
	sort repQtrID issuer_id gvkey  calYrQtr_ef openMrkt calYrQtr rNum match 
	gen timeDiff=calYrQtr_ef-calYrQtr
	gen pri=5
	replace pri=4 if timeDiff==1
	replace pri=3 if timeDiff==2
	replace pri=2 if timeDiff==3
	replace pri=1 if timeDiff==4
	egen matchPri=group(repQtrID issuer_id gvkey calYrQtr_ef openMrkt  match)
	rename (indCode gvkey calYrQtr_ef acq atq lev issuer_id ret1Qtr openMrkt repQtrID) (indCode_1 gvkey_1 calYrQtr_ef_1 acq_1 atq_1 lev_1 issuer_id_1 ret1Qtr_1 openMrkt_1 repQtrID_1)
	rename (matches) (ctrlObs)
tempfile treatSample
save `treatSample'
clear
use fullAcqMatches_repMth
	keep if acq==0
	rename (matches rNum) (treatObs ctrlObs)
	keep ctrlObs indCode gvkey calYrQtr_ef acq atq lev issuer_id ret1Qtr openMrkt repQtrID
	duplicates drop
tempfile ctrlSample
save `ctrlSample'	
clear
use	`treatSample'
	merge m:1 ctrlObs using `ctrlSample', keepusing(indCode gvkey calYrQtr_ef acq atq lev ret1Qtr issuer_id openMrkt repQtrID)	 	
	keep if _merge ==3
	drop _merge
	sort  matchPri repQtrID issuer_id_1 gvkey_1  calYrQtr_ef_1 openMrkt_1 calYrQtr  rNum match 
	drop if issuer_id_1==issuer_id & gvkey_1==gvkey
	gen ctrlRepTime = (calYrQtr_ef_1-calYrQtr_ef)
	keep if ctrlRepTime>=0&ctrlRepTime<=4
	
	sort matchPri timeDiff
	bysort matchPri: egen maxPri=max(pri)
	keep if pri==maxPri
	drop timeDiff pri maxPri
	sort issuer_id_1 gvkey calYrQtr_ef_1 match
	by issuer_id_1 gvkey calYrQtr_ef_1: keep if  _n==1
tempfile matched	
save `matched'
	keep issuer_id_1 gvkey_1 calYrQtr_ef_1 openMrkt_1 acq_1 repQtrID_1
	rename (issuer_id_1 gvkey_1 calYrQtr_ef_1 openMrkt_1 acq_1 ) (issuer_id gvkey calYrQtr_ef openMrkt acq)
	gen treated=1
tempfile treated
save `treated'
clear
use `matched'
	keep issuer_id gvkey calYrQtr_ef openMrkt acq repQtrID_1
	gen treated=0
	duplicates drop 
	append using `treated'
	keep issuer_id gvkey calYrQtr_ef openMrkt acq repQtrID_1 treated
	duplicates drop 
save matchedAcqReps_repMth, replace


clear
use repCS_b4Aftr_wFin
	tab repB4MnA_trg
	gen fyear = year(effective_date)
	bysort issuer_id gvkey calYrQtr_ef: egen trg=sum(repB4MnA_trg)
	replace trg=1 if trg>0
	sort  gvkey calYrQtr_ef
	tab trg
	keep issuer_id gvkey calYrQtr_ef trg indCode fyear openMrkt 
	duplicates drop
	joinby gvkey using `compuData'

	gen treatQtr = cond(calYrQtr==calYrQtr_ef-4| calYrQtr==calYrQtr_ef-3 | calYrQtr==calYrQtr_ef-2 | calYrQtr==calYrQtr_ef-1 | calYrQtr==calYrQtr_ef ,1,0) 
	drop if treatQtr==0
	gsort -trg gvkey calYrQtr_ef calYrQtr 
	drop treatQtr 
	gen lnAtq=ln(1+atq) 
	sort issuer_id gvkey calYrQtr_ef calYrQtr
	
	capture teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (trg), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) osample(check) nn(1)
	tab check
	tab check if trg==1
	drop if check==1
	drop check

	capture teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (trg), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) osample(check) nn(1)
	tab check
	tab check if trg==1
	drop if check==1
	drop check
	
	capture teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (trg), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) osample(check) nn(1)
	tab check
	tab check if trg==1
	drop if check==1
	drop check	

	gsort -trg issuer_id gvkey calYrQtr_ef calYrQtr 
	gen rNum=_n	
	teffects nnmatch (ret1Qtr lnAtq lev cash_at tobinsq) (trg), control(0) e(indCode calYrQtr openMrkt)  caliper(0.5) biasadj(lnAtq lev cash_at tobinsq) gen(matches) nn(1)		
	reshape long matches, i(issuer_id gvkey calYrQtr_ef openMrkt  calYrQtr) j(match) 
	sort rNum issuer_id gvkey calYrQtr_ef openMrkt calYrQtr match
	drop if matches==.
	egen repQtrID=group(issuer_id gvkey calYrQtr_ef openMrkt)
save fullTrgMatches_repMth, replace


clear
use fullTrgMatches_repMth
	keep if trg==1
	sort repQtrID issuer_id gvkey  calYrQtr_ef openMrkt calYrQtr rNum match 
	gen timeDiff=calYrQtr_ef-calYrQtr
	gen pri=5
	replace pri=4 if timeDiff==1
	replace pri=3 if timeDiff==2
	replace pri=2 if timeDiff==3
	replace pri=1 if timeDiff==4
	egen matchPri=group(repQtrID issuer_id gvkey calYrQtr_ef openMrkt  match)
	rename (indCode gvkey calYrQtr_ef trg atq lev issuer_id ret1Qtr openMrkt repQtrID) (indCode_1 gvkey_1 calYrQtr_ef_1 trg_1 atq_1 lev_1 issuer_id_1 ret1Qtr_1 openMrkt_1 repQtrID_1)
	rename (matches) (ctrlObs)
tempfile treatSample
save `treatSample'
clear
use fullTrgMatches_repMth
	keep if trg==0
	rename (matches rNum) (treatObs ctrlObs)
	keep ctrlObs indCode gvkey calYrQtr_ef trg atq lev issuer_id ret1Qtr openMrkt repQtrID
	duplicates drop
tempfile ctrlSample
save `ctrlSample'	
clear
use	`treatSample'
	merge m:1 ctrlObs using `ctrlSample', keepusing(indCode gvkey calYrQtr_ef trg atq lev ret1Qtr issuer_id openMrkt repQtrID)	 	
	keep if _merge ==3
	drop _merge
	sort  matchPri repQtrID issuer_id_1 gvkey_1  calYrQtr_ef_1 openMrkt_1 calYrQtr  rNum match 
	drop if issuer_id_1==issuer_id & gvkey_1==gvkey
	gen ctrlRepTime = (calYrQtr_ef_1-calYrQtr_ef)
	keep if ctrlRepTime>=0&ctrlRepTime<=4
	
	sort matchPri timeDiff
	bysort matchPri: egen maxPri=max(pri)
	keep if pri==maxPri
	drop timeDiff pri maxPri
	sort issuer_id_1 gvkey calYrQtr_ef_1 match
	by issuer_id_1 gvkey calYrQtr_ef_1: keep if  _n==1
tempfile matched	
save `matched'
	keep issuer_id_1 gvkey_1 calYrQtr_ef_1 openMrkt_1 trg_1 repQtrID_1
	rename (issuer_id_1 gvkey_1 calYrQtr_ef_1 openMrkt_1 trg_1 ) (issuer_id gvkey calYrQtr_ef openMrkt trg)
	gen treated=1
tempfile treated
save `treated'
clear
use `matched'
	keep issuer_id gvkey calYrQtr_ef openMrkt trg repQtrID_1
	gen treated=0
	duplicates drop 
	append using `treated'
	keep issuer_id gvkey calYrQtr_ef openMrkt trg repQtrID_1 treated
	duplicates drop 
save matchedTrgReps_repMth, replace
* Combine matched acquirer and target files 
clear
use matchedAcqReps_repMth
	append using matchedTrgReps_repMth
	duplicates drop 	
save matchedReps, replace
	
	
	
*********************************************************************************
* Merger wave identification
*********************************************************************************

clear
use sdcMnA_linked
	keep if trgtnation=="United States" & acqnation=="United States" & pctsought=="100.0"
	drop sameInd48
	sort trgtcusip dateannounced
	gen annYrMth=ym(year(dateannounced),month(dateannounced))
	format %tm annYrMth
	duplicates drop trgtname acqname yrMth trg_indCode48  acq_indCode48 , force
	
	gen sameInd48=cond(trg_indCode48==acq_indCode48,1,0)
tempfile transactions
save `transactions'
	duplicates drop trgtname yrMth trg_indCode48  , force
	drop if trgtname==trgtname[_n-1] & yrMth==yrMth[_n-1]  
	keep yrMth trg_indCode48  
	gen num=1
	rename (trg_indCode48 trg_indCode) (indCode48 )
tempfile trgSide	
save `trgSide'
clear
use `transactions'
	keep if sameInd48==0	
	keep yrMth acq_indCode48 
	rename (acq_indCode48 acq_indCode) (indCode48 )
	gen num=1
	append using `trgSide'	
	collapse  (sum)noBids=num, by(yrMth indCode48 )		
	sort indCode48 yrMth
	by indCode48: egen totBids=sum(noBids)
	sort indCode48 yrMth 
	tsset indCode48 yrMth 
	rangestat  (sum) noBids_24m=noBids, interval(yrMth -23 0) by(indCode48)
	gen yr=year(dofm(yrMth))
	gen period =1
	replace period=2 if yr>1999 & yr<2008
	replace period=3 if yr>=2008
	gen periodMths = .
	replace periodMths = 120 if period==1
	replace periodMths = 96 if period==2
	replace periodMths = 144 if period==3
	
	sort indCode48 period yrMth 
	by indCode48 period: egen totBids_period=sum(noBids)
	gen pct_bids=(noBids/totBids_period)
	gen pct_bids_24=(noBids_24m/totBids_period)
	su pct_bids pct_bids_24 totBids_period
	by indCode48: su totBids_period
	by indCode48 period: egen sd_period=sd(noBids)
save simBase, replace	
	
clear
use simBase	
	* Simulations code segment
	forvalues i=1(1)10000{
		local seedVal = 123456789+`i'
		*di "`seedVal'" 
		set seed `seedVal'
		gen iter`i'=abs(rnormal(totBids_period/periodMths,sd_period))
		bysort indCode48 period: egen iter`i'_sum=sum(iter`i')
		replace iter`i'=round((iter`i'/iter`i'_sum)*totBids_period)
		bysort indCode48 period: egen iter`i'_sum1=sum(iter`i')
		gen dev`i'=totBids_period-iter`i'_sum1
		bysort indCode48 period: gen rnd4Adj`i'=runiform()
		sort indCode48 period rnd4Adj`i'
		by indCode48 period: gen obs4Adj`i'=_n<=abs(dev`i')
		gen iter`i'_adj=iter`i'
		replace iter`i'_adj=iter`i'+(dev`i'/abs(dev`i')) if obs4Adj`i'==1 & iter`i'_adj>0
		drop iter`i' iter`i'_sum iter`i'_sum1 dev`i' rnd4Adj`i' obs4Adj`i' 	
		
	}
	
	sort indCode48 yrMth 
	tsset indCode48 yrMth 
	forvalues i=1(1)10000{
		rangestat  (sum) noBids`i'_24m=iter`i'_adj, interval(yrMth -23 0) by(indCode48)
	}
	drop iter*_adj
	rename noBids_24m org_noBids_24m
	egen sim_Bids95th=rowpctile(noBids*_24m), p(95)
	drop noBids*_24m
save simulated, replace		

clear all
set obs 24
	gen period=.
	forvalues i= 1/24{
		replace period=-`i' if _n==`i'
	}
	replace period=period+1
	gen id=1
tempfile prePeriod	
save `prePeriod'

clear
use simulated
	replace period=. if yr<1993 & period==1
	*drop if period==.
	sort indCode48 period yrMth
	by indCode48 period: egen maxBid=max(org_noBids_24m) if period!=.
	gen potWave=cond(org_noBids_24m==maxBid,1,0)
	gen wave=cond(org_noBids_24m>=sim_Bids95th & potWave==1,1,0)
	sort indCode48 yrMth
tempfile waveData
save `waveData'
	keep if wave==1
	keep indCode48 yrMth wave
	gen id=1
	joinby id using `prePeriod'
	sort indCode48 yrMth wave period
	gen waveMth=cond(period==0,1,0)
	replace yrMth=yrMth+period
	rename wave wavePeriod
	drop id period waveMth
	duplicates drop 
save wavePeriod, replace
* Prep M&A Wave data
clear
use wavePeriod
	gen calYrQtr_ef = yq(year(dofm(yrMth)), quarter(dofm(yrMth)))
	format %tq calYrQtr_ef
	drop yrMth
	duplicates drop 
save wave, replace	
	
*********************************************************************************
* Prep M&A data: Identify Xindustry and Xcountry acquirers & targets
*********************************************************************************

clear
use sdcMnA_linked
	gen cross_country=cond(trim(lower(acqnation))!=trim(lower(trgtnation)),1,0)
	tab cross_country
	keep permco_acq permco_trgt gvkey_acq gvkey_trgt  dateannounced diff_sic4 diff_sic3 cross_country
	gen calYrQtr_ef=qofd(dateannounced)
	format %tq calYrQtr_ef
	gen deal=1
save base_file, replace
	collapse (sum) deal diff_sic4 diff_sic3 cross_country, by(permco_acq calYrQtr_ef)
	rename (permco_acq diff_sic4 diff_sic3 cross_country calYrQtr_ef deal) (permco diff_sic4_acq diff_sic3_acq cross_country_acq calYrQtr_ef_acq cross_acquirer)
tempfile acq_file	
save `acq_file'
use base_file, clear
	collapse (sum) diff_sic4 diff_sic3 cross_country deal, by(permco_trgt gvkey_trgt calYrQtr_ef)
	rename (permco_trgt diff_sic4 diff_sic3 cross_country calYrQtr_ef deal) (permco diff_sic4_trg diff_sic3_trg cross_country_trg calYrQtr_ef_trg cross_target)
tempfile trg_file	
save `trg_file'
clear
use repCS_b4Aftr_wFin
	keep permco calYrQtr_ef
	duplicates drop 
	joinby permco using `acq_file'
	gen repB4MnA_acq=cond(calYrQtr_ef-calYrQtr_ef_acq>=0&calYrQtr_ef-calYrQtr_ef_acq<=8,1,0)
	keep if repB4MnA_acq==1
	collapse (sum) cross_acquirer diff_sic4_acq diff_sic3_acq cross_country_acq, by(permco calYrQtr_ef)
save acq_within_2yrs, replace
clear
use repCS_b4Aftr_wFin
	keep permco calYrQtr_ef
	duplicates drop 
	joinby permco using `trg_file'
	gen repB4MnA_trg=cond(calYrQtr_ef-calYrQtr_ef_trg>=0&calYrQtr_ef-calYrQtr_ef_trg<=8,1,0)
	keep if repB4MnA_trg==1
	collapse (sum) cross_target diff_sic4_trg diff_sic3_trg cross_country_trg, by(permco calYrQtr_ef)
save trg_within_2yrs

* Prep NTR gap data
* Extend sample
clear
set obs 20
	gen rep_yr=2001
	forvalues i=1(1)20{
		replace rep_yr=rep_yr+`i' if _n==`i'
	}
tempfile extra_yrs	
save `extra_yrs'
clear
import delimited D:\Dropbox\Datasets\TRACE\data\tariff\NTRgap_NAICSoriginal_PeterSchott_v2.csv
	replace sic =trim(sic)
	replace naics_code=trim(naics_code)
	gen sic1=real(sic)
	gen naics_code1=real(naics_code)
	drop sic naics_code
	rename (sic1 naics_code1 ) (sic4 naics_code )
	destring ntrgap, replace i('N' 'A')
	sort sic4 rep_yr	
	drop if sic4==.
	collapse (mean) ntrgap, by(sic4 rep_yr)
	append using `extra_yrs' 
	fillin sic4 rep_yr
	sort sic4 rep_yr
	tsset sic4 rep_yr
	replace ntrgap=l.ntrgap if ntrgap==.
	keep sic4 rep_yr ntrgap 
	rename (rep_yr sic4) (calYr sic)
save ntrgap, save

* Identify acquirers who benefited from Cloud computing
use sdcMnA_linked, clear
	gen internet=0
	replace internet=1 if acqindsector=="Advertising Services"|	acqindsector=="Air Transportation and Shipping"|	acqindsector=="Amusement and Recreation Services"|	acqindsector=="Business Services"|	acqindsector=="Commercial Banks, Bank Holding Companies"|	acqindsector=="Computer and Office Equipment"|	acqindsector=="Credit Institutions"|	acqindsector=="Educational Services"|	acqindsector=="Insurance"|	acqindsector=="Investment & Commodity Firms,Dealers,Exchanges"|	acqindsector=="Legal Services"|	acqindsector=="Motion Picture Production and Distribution"|	acqindsector=="Other Financial"|	acqindsector=="Prepackaged Software"|	acqindsector=="Printing, Publishing, and Allied Services"|	acqindsector=="Radio and Television Broadcasting Stations"|	acqindsector=="Real Estate; Mortgage Bankers and Brokers"|	acqindsector=="Savings and Loans, Mutual Savings Banks"	
	collapse (sum) internet (count) no_acq=acq_indCode, by(acqprisic yr)	
	replace internet=1 if internet>0	
	keep if internet==1
	gen sic=real(acqprisic)
	drop if sic==.
	keep sic internet
	duplicates drop 
save internet_sics, replace

* Get industry
use permco sic_code indCode indCode48 using fisdSample_MnA_TRACE_cov_grpd, clear
	duplicates drop permco, force
	destring sic_code, gen(sic)	
save industry, replace

********************************************************************************
* Calculate returns
********************************************************************************
clear
use permco using fisProcess
	duplicates drop 
tempfile permcos	
save `permcos'
use PERMCO PERMNO date SHRCD RET RETX vwretd vwretx ewretd ewretx using crsp, clear
	rename (PERMCO PERMNO RET SHRCD) (permco permno ret shrcd)
	merge m:1 permco using `permcos'
	keep if _merge==3
	drop _merge
	sort permno date
	by permno: gen rNum=_n
	winsor2 ret vwretd ewretd, replace cuts(1 99)	
	assert ret > -1 if !missing(ret)
	gen lnRet=ln(1+ret)
	gen lnRet_vw=ln(1+(ret-vwretd))
	gen lnRet_ew=ln(1+(ret-ewretd))

	rangestat (count) retCounts=lnRet (sum) ret1=lnRet ret1_vw=lnRet_vw ret1_ew=lnRet_ew, interval(rNum -1 1) by(permno)
	rangestat (sum) ret3=lnRet ret3_vw=lnRet_vw ret3_ew=lnRet_ew, interval(rNum -3 3) by(permno)
	rangestat (sum) ret30=lnRet ret30_vw=lnRet_vw ret30_ew=lnRet_ew, interval(rNum -30 30) by(permno)
	rangestat (sum) ret60=lnRet ret60_vw=lnRet_vw ret60_ew=lnRet_ew, interval(rNum -60 60) by(permno)

	gen cum_ret1=(exp(ret1)-1)*100
	gen cum_ret1_vw=(exp(ret1_vw)-1)*100
	gen cum_ret1_ew=(exp(ret1_ew)-1)*100

	gen cum_ret3=(exp(ret3)-1)*100
	gen cum_ret3_vw=(exp(ret3_vw)-1)*100
	gen cum_ret3_ew=(exp(ret3_ew)-1)*100

	gen cum_ret30=(exp(ret30)-1)*100
	gen cum_ret30_vw=(exp(ret30_vw)-1)*100
	gen cum_ret30_ew=(exp(ret30_ew)-1)*100

	gen cum_ret60=(exp(ret60)-1)*100
	gen cum_ret60_vw=(exp(ret60_vw)-1)*100
	gen cum_ret60_ew=(exp(ret60_ew)-1)*100
save permcoPrices_bnd_frms, replace

clear
use permcoPrices_bnd_frms
	keep if (shrcd)==10 | (shrcd)==11
	rename date newDate
	collapse (mean) cum_ret1 cum_ret1_vw cum_ret1_ew cum_ret3 cum_ret3_vw cum_ret3_ew  cum_ret30 cum_ret30_vw cum_ret30_ew cum_ret60 cum_ret60_vw cum_ret60_ew, by(permco newDate)
save stockReturns_bnd_frms, replace

****************************************************************************************************************************************************************
* Data Prep: Get CDS spreads and liquidity for +-30 and +-60 days 
****************************************************************************************************************************************************************



* Select US CDS sample
clear
use "D:\Datasets\TRACE\raw\cds.dta"
	keep if country=="United States" &  ccy=="USD" 
	keep if tier=="SNRFOR"
	keep if docclause=="MR" | docclause=="MR14" | docclause=="XR" | docclause=="XR14"
	sort shortname date redcode	
save cds_us_fltrd, replace


* Drop CDSs that map to multiple GVKEYs
clear
use gvkey_redcode_linktable
	bysort redcode: gen dup=cond(_N>1,1,0)
	drop if dup>0
	drop dup
tempfile linkFile	
save `linkFile'

* Merge Compustat-CRSP-Markit CDS Link file with Markit US sample
clear
use cds_us_fltrd
	duplicates drop redcode date tier docclause, force
	merge m:1 redcode using `linkFile', keepusing(gvkey tic)	
	keep if _merge==3
	drop _merge
	sort gvkey date
	gen mr=cond(docclause=="MR" | docclause=="MR14",1,0)
	gen xr=cond(docclause=="XR" | docclause=="XR14",1,0)
	bysort gvkey date: egen mr_all=sum(mr)
	bysort gvkey date: egen xr_all=sum(xr)
	gsort -mr_all gvkey date docclause
	tab mr_all
	gen rating = 0
	replace rating = 1 if impliedrating=="D"
	replace rating = 2 if impliedrating=="CCC"
	replace rating = 3 if impliedrating=="BBB"
	replace rating = 4 if impliedrating=="BB"
	replace rating = 5 if impliedrating=="B"
	replace rating = 6 if impliedrating=="A"
	replace rating = 7 if impliedrating=="AA"
	replace rating = 8 if impliedrating=="AAA"
	collapse (mean) spread6m spread1y spread2y spread3y spread4y spread5y spread7y spread10y spread15y spread20y spread30y (max) recovery compositedepth5y rating, by(gvkey date)
save cds_us_daily, replace

* Figure our firms with bonds
clear
use gvkey using fisdSample_MnA_TRACE_cov
	duplicates drop
tempfile bondFirms	
save `bondFirms'
* Dates for a panel
clear
set obs 7306
	gen date=mdy(12,31,2000)
	format date %td
	forvalues i=1(1)7306{
		replace date=date+`i' if _n==`i'
	}
	gen hook=1
tempfile dates
save `dates'
* Create CDS-Day panel
clear
use gvkey date using cds_us_daily
	collapse (min) minDate=date (max) maxDate=date, by(gvkey)
	gen hook=1
	* Add Bond firms
	merge m:1 gvkey using `bondFirms'	
	keep if _merge==3
	drop _merge

	joinby hook using `dates'
	keep if date>=minDate & date<=maxDate
	drop hook minDate maxDate

	* Add CDS data
	merge 1:1 gvkey date using cds_us_daily
	drop if _merge==2
	drop _merge
	sort gvkey date 
	local vars spread6m spread1y spread2y spread3y spread4y spread5y spread7y spread10y spread15y spread20y spread30y recovery compositedepth5y rating
	foreach v of local vars{
		by gvkey: replace `v'=`v'[_n-1] if `v'==. 
	}
	rangestat (mean) spread1y_me30=spread1y  spread5y_me30=spread5y spread10y_me30=spread10y liq30=compositedepth5y, interval(date 0 30) by(gvkey)
	rangestat (mean) spread1y_me60=spread1y  spread5y_me60=spread5y spread10y_me60=spread10y liq60=compositedepth5y, interval(date 0 60) by(gvkey)
	gen slope30=spread10y_me30-spread1y_me30 if spread1y_me30!=. & spread10y_me30!=.
	gen slope60=spread10y_me60-spread1y_me60 if spread1y_me60!=. & spread10y_me60!=.
	rangestat (mean) spread1y_me30_pm =spread1y  spread5y_me30_pm=spread5y spread10y_me30_pm=spread10y liq30_30=compositedepth5y, interval(date -30 30) by(gvkey)
	rangestat (mean) spread1y_me60_pm=spread1y  spread5y_me60_pm=spread5y spread10y_me60_pm=spread10y liq60_60=compositedepth5y, interval(date -60 60) by(gvkey)
	gen slope30_pm=spread10y_me30_pm-spread1y_me30_pm if spread1y_me30_pm!=. & spread10y_me30_pm!=.
	gen slope60_pm=spread10y_me60_pm-spread1y_me60_pm if spread1y_me60_pm!=. & spread10y_me60_pm!=.
	gen slope30_pm_5_1=spread5y_me30_pm-spread1y_me30_pm if spread1y_me30_pm!=. & spread5y_me30_pm!=.
	gen slope60_pm_5_1=spread5y_me60_pm-spread1y_me60_pm if spread1y_me60_pm!=. & spread5y_me60_pm!=.
	rangestat (mean) spread1y_me1_pm =spread1y  spread5y_me1_pm=spread5y spread10y_me1_pm=spread10y liq1_1=compositedepth5y, interval(date -1 1) by(gvkey)
	rangestat (mean) spread1y_me3_pm=spread1y  spread5y_me3_pm=spread5y spread10y_me3_pm=spread10y liq3_3=compositedepth5y, interval(date -3 3) by(gvkey)
	gen slope1_pm=spread10y_me1_pm-spread1y_me1_pm if spread1y_me1_pm!=. & spread10y_me1_pm!=.
	gen slope3_pm=spread10y_me3_pm-spread1y_me3_pm if spread1y_me3_pm!=. & spread10y_me3_pm!=.
	gen gvkeyNo=real(gvkey)
	tsset gvkeyNo date
	gen spred_diff_1=f.spread5y-l.spread5y
	gen spred_diff_3=f3.spread5y-l3.spread5y
	gen spred_diff_30=f30.spread5y-l30.spread5y
	gen spred_diff_60=f60.spread5y-l60.spread5y
	gen liq_diff_1=f.compositedepth5y-l.compositedepth5y
	gen liq_diff_3=f3.compositedepth5y-l3.compositedepth5y
	gen liq_diff_30=f30.compositedepth5y-l30.compositedepth5y
	gen liq_diff_60=f60.compositedepth5y-l60.compositedepth5y
save cds_us_daily_sprds, replace

* Figure out repurchasing firms
clear
use gvkey effective_date effective_date_hist repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res openMrkt using fisdSample_MnA_TRACE_cov
	keep if repurchase==1
	replace effective_date_hist = effective_date if effective_date_hist ==.
	su effective_date effective_date_hist 
	*keep gvkey effective_date_hist repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res 
	rename effective_date_hist  date
	gen toffer=(1-openMrkt)
	collapse (sum) repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res  toffer openMrkt, by(gvkey date )
	local vars repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res openMrkt toffer   
	foreach v of local vars{
		replace `v'=1 if `v'>0
	}
	gen hook=1
	egen repID=group(gvkey date)
tempfile repFirms	
save `repFirms'
clear
use gvkey using `repFirms'
	duplicates drop 
tempfile repGvkeys	
save `repGvkeys'
* Figure out the financial year in which repurchase happened
clear
use gvkey fyear datadate indfmt if indfmt=="INDL" using  compuAnnual
	merge m:1 gvkey using `repGvkeys'
	keep if _merge==3
	drop _merge
	gen hook=1
	gen gvkeyNo=real(gvkey)
	tsset gvkeyNo fyear
	gen prevDate=l.datadate
	format %td prevDate
	joinby gvkey hook using `repFirms'
	keep if date<=datadate & date>prevDate
	sort gvkey fyear date
save repDates_cp, replace		
* Firm-years of repurchases
clear
use repDates_cp
	collapse (sum) repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res toffer openMrkt, by(gvkey fyear)
	local vars repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res toffer openMrkt
	foreach v of local vars{
		replace `v'=1 if `v'>0
	}
tempfile repurchases
save `repurchases'

* Find Start and end dates of all CDS firms 
clear
use gvkey date using cds_us_daily_sprds
	collapse (min) minDate=date (max) maxDate=date, by(gvkey)
tempfile cdsFirms	
save `cdsFirms'	
* Form a Compustat panel of all CDS firms and identify repurchaser firm-years for creating a matched sample
clear
use gvkey fyear at dlc dltt datadate sic sale prcc_f indfmt if indfmt=="INDL" using  compuAnnual
	duplicates drop gvkey fyear, force
	gen gvkeyNo=real(gvkey)
	tsset gvkeyNo fyear
	replace prcc_f=abs(prcc_f)
	gen ret_f=(prcc_f-l.prcc_f)/l.prcc_f
	gen lev=(dlc+dltt)/at
	sort gvkey fyear
	bysort gvkey: ipolate at fyear, g(atq_ipo)
	bysort gvkey: ipolate lev fyear, g(lev_ipo)
	bysort gvkey: ipolate sale fyear, g(sale_ipo)
	
	gsort gvkey -fyear 
	bysort gvkey: replace atq_ipo=atq_ipo[_n-1] if  atq_ipo==.
	bysort gvkey: replace lev_ipo=lev_ipo[_n-1] if  lev_ipo==.
	bysort gvkey: replace sale_ipo=sale_ipo[_n-1] if  sale_ipo==.
	
	sort gvkey fyear 
	bysort gvkey: replace atq_ipo=atq_ipo[_n-1] if  atq_ipo==.
	bysort gvkey: replace lev_ipo=lev_ipo[_n-1] if  lev_ipo==.
	bysort gvkey: replace sale_ipo=sale_ipo[_n-1] if  sale_ipo==.
	
	drop  lev at dlc dltt sale
	rename (atq_ipo lev_ipo sale_ipo) (at lev sale)
	gen ln_at =ln(1+at)
	gen lnLev=ln(1+lev)
	gen lnSale=ln(1+sale)
	merge m:1 gvkey using `cdsFirms'
	keep if _merge==3
	drop _merge
	keep if datadate>=(minDate-365) & datadate<=maxDate
	merge 1:1 gvkey fyear using `repurchases'
	drop if _merge==2
	drop _merge
	local vars repurchase inv_res_umb div_res_umb sub_fin_res_umb event_res_umb merger_res control_change_res toffer openMrkt
	foreach v of local vars{
		replace `v'=0 if `v'==.
	}
	
	egen indYr=group(indCode fyear)
	gen match=.
save cds_firms_panel, replace	

